使用 Log4Net 创建只读日志文件

发布于 2024-10-20 06:10:19 字数 1280 浏览 2 评论 0原文

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

我想将此文件设置为只读,并且 log4net 仍然可以对其进行写入。因为如果我只是更改文件中的属性,则不会写入下一个日志。

有办法做到这一点吗?

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

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

这是创建和调用记录器的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唱一曲作罢 2024-10-27 06:10:19

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

Basically, if the application runs under the rights of the user, than the user ultimately must have the rights to access the files: if he doesn't, the application wouldn't be able to write into this files either.

情深已缘浅 2024-10-27 06:10:19

我会更改正在写入日志的文件夹的权限。 Web 服务器授予读/写权限,其他所有权限均为读权限。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文