Create a ReadOnly log file with Log4Net AND Separate log in 2 files

主宰稳场 提交于 2020-01-07 07:45:24

问题


The first question is:

I'm developing application in c# that creates 2 log files (.txt files): one for errors and another for modifications made by users. This two files are created with log4net. The issue I see is that these files can be edited, and so altered by mistake.

I would like to set these files to readonly, and that log4net still could write to them. Because if I just change the property in the file, the next log won't be written.

Is there a way to do that?

Also, the user of the app can open this logs file from within the app. For that I use the next code:

System.IO.FileInfo finfo = new System.IO.FileInfo("path");
if (finfo.Exists)
{
 //finfo.Attributes = System.IO.FileAttributes.ReadOnly; 
 // I don't use the previous line at the moment, because it blocks the followings logs.
 System.Diagnostics.Process.Start("path");
}

And this is the code to create and call the logger:

public static class CLogger
{
   private static readonly ILog logger = LogManager.GetLogger(typeof(CLogger));

   static CLogger()
   {
      XmlConfigurator.Configure(new System.IO.FileInfo("path to .config file"));
   }

   public static void WriteLog(ELogLevel logLevel, String log)
   {
      if (logLevel.Equals(ELogLevel.DEBUG))
      {
         logger.Debug(log);
      }
      else if (logLevel.Equals(ELogLevel.ERROR))
            .
            .
            .          
      else if (logLevel.Equals(ELogLevel.WARN))
      {
                logger.Warn(log);
      }
   }
}

Calling to the logger:

CLogger.WriteLog(ELogLevel.ERROR, ex.ToString());

And I have a second question related:

To create this 2 separate log files I use the next lines in the .config file:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="PATH...\ErrorLog.log" />
      <param name="AppendToFile" value="true" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="ERROR"/>
        <param name="LevelMax" value="ERROR"/>
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="" />
        <param name="Footer" value="" />
        <param name="ConversionPattern" value="%d [%t] %-5p %username %m%n" />
      </layout>
      <threshold value="ERROR" />
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="PATH...\TraceLog.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO"/>
        <param name="LevelMax" value="INFO"/>
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="" />
        <param name="Footer" value="" />
        <param name="ConversionPattern" value="%d [%t] %-5p %username %m%n" />
      </layout>
      <threshold value="INFO" />
    </appender>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
      </layout>
      <threshold value="ERROR" />
    </appender>
    <root>
      <level value="ERROR" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
      <level value="INFO" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</configuration>

That works, but am I doing that all right? I have doubt whether I should use 'LogFileAppender' or 'RollingFileAppender', that's why I used them both.

Thanks in advance.


回答1:


Question 1: I think that is the wrong way to approach it. If you fear they can modify it by accident, then you have two options:

  • Make a copy of the log file and display that one.
  • Display the log file in a window that you control (i.e. make the text area read-only).

Question 2: The "normal" FileAppender will always write to the same file, this may not be an issue for your application is stopped once in a while and you can delete the log file when grows too large. For long running applications this is not an option: The rolling RollingFileAppender will create a new file every once a while (defined by certain size or certain time). This way you can delete old logfiles and never run out of disk space.

Maybe you want to check out more on appenders here.



来源:https://stackoverflow.com/questions/5117927/create-a-readonly-log-file-with-log4net-and-separate-log-in-2-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!