【问题标题】:logging stack trace with logger使用记录器记录堆栈跟踪
【发布时间】:2011-04-06 05:07:58
【问题描述】:

我正在使用带有 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>

【问题讨论】:

  • 你能发布监听器和源配置吗?
  • 用更多信息编辑了我的帖子
  • 堆栈跟踪中的某些特殊字符是否可能阻止文件写入过程?因为如果我在堆栈跟踪中有超过 50 个字符,则不会创建文件本身?这只是一个猜测......
  • 停止猜测并弄清楚。尝试记录一些硬编码的字符串,然后查看堆栈跟踪中的内容并查看第一个未记录的字符是什么。请注意,更改 Windows 事件日志的配置可能需要重新启动才能获取更改(根据文档)。
  • 同意格雷格。另一个建议是删除事件日志侦听器并仅尝试使用文件侦听器。

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


【解决方案1】:

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

【讨论】:

  • string str = "消息:" + exp.Message + Environment.NewLine + "堆栈跟踪:" + exp.StackTrace.ToString();
  • 我从头开始重新编写了所有代码,发现它正在运行接受超过 50 个字符。不知道以前是怎么失败的!!!需要彻底检查当时的堆栈跟踪..
【解决方案2】:

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

【讨论】:

  • 不是那样的...... LogEntry 是来自命名空间 Microsoft.Practices.EnterpriseLibrary.Logging 的一个类
  • 如果它没有被密封,你仍然可以继承它。微软通常也会很好地设计他们发布的类以进行子类化。事实上,查找它,他们已经对其进行了多次子类化:msdn.microsoft.com/en-us/library/…
  • 感谢您的回复和有用的信息……不知何故,它在再次重写所有代码后开始工作。不知道怎么来的!需要彻底检查早期的堆栈跟踪文本
猜你喜欢
  • 2014-11-01
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
  • 2011-06-23
相关资源
最近更新 更多