我正在使用带有 C#.Net 2.0 的 Logging Application 块。我的代码将错误信息记录到平面文件中。我已经在 web.config 中设置了所有必需的配置,如 msdn 中所述的侦听器、格式化程序和类别等,并且工作正常。但问题是,我不能在 le.Message 属性中放置超过 50 个字符。就我而言,堆栈跟踪的长度超过 500 个字符,我想在发生错误时登录到平面文件。
我们可以在 LogEntry 对象的 Message 属性中放入的字符数量是否有限制?或者有没有其他方法可以将堆栈跟踪记录到记录器平面文件中?
这是简单的代码。
LogEntry le = new LogEntry();
le.Categories.Add("ErrorsToEventLog");
le.Categories.Add("ErrorsToLogFile");
le.Title = "Error message";
le.TimeStamp = System.DateTime.Now;
le.Severity = System.Diagnostics.TraceEventType.Error;
le.Message = "<text of error's stack trace>";
Logger.write(le);
配置设置
<configSections>
<section name="loggingConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=null" />
<section name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=null" />
</configSections>
这是我使用的格式化程序,
<formatters>
<add template="Timestamp: {timestamp} Message: {message}"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=null" name="Text Formatter" />
</formatters>
这是听众,
<add fileName="Logs/ErrorLog_{Date}.log"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.
CustomTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=null" traceOutputOptions="None"
type="EnterpriseLibrary.Logging.Extensions.RollingFlatFileTraceListener,
EnterpriseLibrary.Logging.Extensions, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" name="Custom TraceListener" initializeData="" />
类别
<categorySources>
<add switchValue="All" name="ErrorsToEventLog">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</add>
<add switchValue="All" name="ErrorsToLogFile">
<listeners>
<add name="Custom TraceListener" />
</listeners>
</add>
</categorySources>