使用记录器记录堆栈跟踪

时间:2010-08-31 10:49:14

标签: c# .net error-logging logging-application-block

我正在使用带有C#.Net 2.0的Logging Application块。我的代码将错误信​​息记录到平面文件中。我已经在web.config中设置了所有必需的配置,如监听器,格式化程序和类别等,如msdn中所述,它工作正常。 但问题是,我不能在le.Message属性中放置超过50个字符。在我的情况下,堆栈跟踪超过500个字符长,我想在发生错误时登录到平面文件。

我们可以在LogEntry对象的Message Property中放置的charactors数量有限制吗?或者有没有其他方法将堆栈跟踪记录到logger平面文件中?

这是简单的代码。

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>

2 个答案:

答案 0 :(得分:1)

据我所知,日志消息没有这样的限制。如何将堆栈跟踪设置为消息?

答案 1 :(得分:0)

假设你的分析是正确的(现在我不方便仔细检查),你是否考虑过为LogEntry创建一个没有你正在运行的限制的子类?

相关问题