【发布时间】:2011-03-01 18:03:21
【问题描述】:
我正在用 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());
【问题讨论】: