简体   繁体   中英

Create a ReadOnly log file with 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 this files can be edited, and so altered by mistake.

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

There is 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());

基本上,如果应用程序在用户权限下运行,则用户最终必须拥有访问文件的权限:如果他没有,则应用程序也无法写入此文件。

I would change the rights on the folder where the logs are being written. The web server granted Read/Write, all other only Read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM