使用Log4Net创建一个ReadOnly日志文件,并分别记录2个文件 [英] Create a ReadOnly log file with Log4Net AND Separate log in 2 files

查看:111
本文介绍了使用Log4Net创建一个ReadOnly日志文件,并分别记录2个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题是:

我正在用c#开发应用程序,该应用程序创建2个日志文件(.txt文件):一个用于错误,另一个用于用户修改.这两个文件是使用 log4net 创建的.我看到的问题是这些文件可以编辑,因此会被错误地更改.

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.

我想将这些文件设置为只读,而log4net仍然可以写入它们.因为如果我仅更改文件中的属性,则不会写入下一个日志.

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.

有办法吗?

此外,应用程序的用户可以从应用程序内部打开此日志文件.为此,我使用下一个代码:

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);
      }
   }
}

致电记录器:

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

我有一个与之相关的第二个问题:

要创建这2个单独的日志文件,请使用.config文件中的下一行:

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>

那行得通,但是我可以吗? 我怀疑我应该使用' LogFileAppender '还是' RollingFileAppender ',这就是为什么我要同时使用它们.

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.

谢谢.

推荐答案

问题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:

  • 复制日志文件并显示该文件.
  • 在您控制的窗口中显示日志文件(即,使文本区域为只读).

问题2: 正常" FileAppender将始终写入同一文件,这可能不成问题,因为您的应用程序偶尔会停止,并且当文件过大时可以删除该日志文件.对于长时间运行的应用程序,这不是一个选择:滚动的RollingFileAppender将每隔一段时间(由特定大小或特定时间定义)创建一个新文件.这样,您可以删除旧的日志文件,而永远不会用完磁盘空间.

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.

这篇关于使用Log4Net创建一个ReadOnly日志文件,并分别记录2个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆