我使用log4net为我的应用程序写入日志文件。我已将日志文件路径设置如下:
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="D:\MyApp\LogFiles\MyApp_"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
.
.
.
日志文件保存在D盘中。如果D驱动器(默认日志文件位置)不可用/不存在,如何更改为C驱动器?是否可以在编码(C#)中这样做,或者我没有选择强迫其他用户拥有D驱动器?
答案 0 :(得分:1)
appender可以在代码中配置而不是使用配置文件,因此很容易确定日志文件应放在代码中的哪个驱动器。更多细节请参阅another thread。
答案 1 :(得分:0)
通过结合@Simonzhao提供的参考,解决方案将如下所示:
public static Logger()
{
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date %-5level %message%newline";
patternLayout.ActivateOptions();
RollingFileAppender rollingFileAppender = new RollingFileAppender();
rollingFileAppender.AppendToFile = true;
#region Write the log file into D drive, if D drive is not found, then into E drive, else C drive
var diskDrive = DriveInfo.GetDrives();
if (diskDrive.Where(drive => drive.Name == "D:\\").Count() == 1)
rollingFileAppender.File = @"D:\LogFiles\MyApp_";
else if (diskDrive.Where(drive => drive.Name == "E:\\").Count() == 1)
rollingFileAppender.File = @"E:\LogsFiles\MyApp_";
else
rollingFileAppender.File = @"C:\LogFiles\MyApp_";
#endregion
rollingFileAppender.Layout = patternLayout;
rollingFileAppender.MaxSizeRollBackups = 5;
rollingFileAppender.MaximumFileSize = "5MB";
.
.
.
}