C# 使用记录器记录堆栈跟踪

C# 使用记录器记录堆栈跟踪,c#,.net,error-logging,logging-application-block,C#,.net,Error Logging,Logging Application Block,我正在使用C#.Net 2.0的日志应用程序块。我的代码正在将错误信息记录到平面文件中。我已经在web.config中设置了所有必需的配置,如侦听器、格式化程序和类别等,如msdn中所述,并且工作正常。 但问题是,我不能在le.Message属性中放置超过50个字符。在我的例子中,堆栈跟踪长度超过500个字符,我希望在发生错误时将其登录到平面文件中 我们可以在LogEntry对象的Message属性中放置的字符数量有限制吗?或者是否有其他方法将堆栈跟踪记录到记录器平面文件中 下面是简单的代码 L

我正在使用C#.Net 2.0的日志应用程序块。我的代码正在将错误信息记录到平面文件中。我已经在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);
LogEntry le=newlogentry();
添加(“错误事件日志”);
添加(“错误日志文件”);
le.Title=“错误消息”;
le.TimeStamp=System.DateTime.Now;
le.Severity=System.Diagnostics.TraceEventType.Error;

le.Message=“据我所知,日志消息没有这样的限制。如何设置消息的堆栈跟踪?

假设您的分析是正确的(我现在不方便再次检查),您是否考虑过为LogEntry创建一个不受限制的子类?

您可以发布侦听器和源配置吗?使用更多信息编辑我的帖子是否可能堆栈跟踪中的某些特殊字符阻止了文件写入过程?因为如果堆栈跟踪中的字符超过50个,则未创建e文件本身?这只是猜测…停止猜测并找出答案。尝试记录一些硬编码字符串,然后查看堆栈跟踪中的内容,并查看第一个未记录的字符是什么。请注意,更改windows事件日志的配置可能需要重新启动以获取更改(根据文档)。同意Greg。另一个建议是删除事件日志侦听器,并仅使用文件listener.string str=“Message:+exp.Message+Environment.NewLine+”堆栈跟踪:“+exp.StackTrace.ToString();我从头开始重新编写了所有代码,发现它的运行接受了50多个字符。不知道以前是如何失败的!!!需要彻底检查当时的堆栈跟踪..不是这样..LogEntry是命名空间Microsoft.Practices.EnterpriseLibrary.Logging中的一个类如果它没有密封,您仍然可以对它进行子类化。微软通常在为子类化设计其发布的类方面也做得很好。事实上,在查找时,他们已经对其子类化了好几次:感谢回复和有用的信息…在重新编写所有代码后,不知何故,它开始工作了。不知道为什么!需要检查早期的堆栈跟踪tex吗彻底地
<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>