1

I'm using log4net to write log file for my application. I've set the log file path as below:

<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"/>
                    .  
                    .
                    .

The log file is saved in D drive. How can I change to C drive if D drive (default log file location) is not available/not exist? Is it possible to do so in the coding (C#) or I've no choice to force other users to have D drive?

YWah
  • 571
  • 13
  • 38

2 Answers2

1

The appender is able to be configured in code instead of using of a config file so that it's quite easy to determine which drive the log file should be put in code. More detail please refer to another thread.

Community
  • 1
  • 1
Simon zhao
  • 56
  • 7
  • Hi @Simonzhao, thanks for the reference...i've combined it with the disk drive checking and it work well now =)... – YWah Aug 14 '15 at 00:21
0

By the combining the reference that @Simonzhao provided, the solution will be looked like:

        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";
                          .
                          .
                          .
       }
YWah
  • 571
  • 13
  • 38