我有以下代码将日志项写入文件。它正确地创建文件,但文件中从不存在任何内容(即它总是大小为0字节)。我做错了什么?
ILog log = LogManager.GetLogger("mainlog");
RollingFileAppender logAppender = new RollingFileAppender();
string filePath = ConfigurationManager.AppSettings["LoggingFilePath"];
filePath = filePath + "EventLog.csv";
logAppender.File = filePath;
logAppender.AppendToFile = true;
logAppender.RollingStyle = RollingFileAppender.RollingMode.Date;
logAppender.MaxSizeRollBackups = 60;
logAppender.StaticLogFileName = true;
log4net.Config.BasicConfigurator.Configure(logAppender);
logAppender.ActivateOptions();
StringBuilder sb = new StringBuilder();
sb.Append(DateTime.Now.ToShortDateString());
sb.Append(",");
sb.Append(DateTime.Now.ToShortTimeString());
sb.Append(",");
sb.Append(applicationID.ToString());
sb.Append(",");
sb.Append(itemName);
sb.Append(",");
sb.Append(itemException.Message);
sb.Append(",");
sb.Append(itemException.StackTrace);
sb.Append(",");
sb.Append(itemException.Source);
sb.Append(",");
sb.Append(itemException.TargetSite);
sb.Append(",");
sb.Append(itemEventType.ToString());
log.Debug(sb.ToString());
logAppender.Close();
答案 0 :(得分:0)
您需要指定布局,例如
logAppender.Layout = new log4net.Layout.PatternLayout(
"%date [%thread] %-5level %logger [%property{NDC}] - %message%newline");