【问题标题】:How to prevent truncation of service exception in event log如何防止在事件日志中截断服务异常
【发布时间】:2014-06-22 16:17:15
【问题描述】:

我们有一个 c# windows 服务失败,导致错误日志消息写入 windows 事件日志。错误消息包含异常信息,但被截断,使我们无法看到关键信息。

我们如何增加写入日志消息的数据,以便我们可以看到异常的完整堆栈跟踪?

这是我们目前在事件查看器中看到的。

<EventData>
    <Data>Service cannot be started. System.ArgumentException: Keyword not supported: 'port'. 
 at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) 
 at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) 
 at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) 
 at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) 
 at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) 
 at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<SetConnectionString>b__1a(DbConnection t...</Data> 
    </EventData>
</Event>

注意堆栈跟踪的结尾被“...”截断。

【问题讨论】:

  • 你有没有想过如何获得这些异常的完整堆栈跟踪?我有同样的问题,这个未回答的问题是我能找到的唯一相关的谷歌结果!
  • 不,我们必须实现自定义日志记录。
  • 谢谢。我们自己也做过类似的事情。在这方面找不到谷歌结果真是太奇怪了!

标签: c# windows windows-services event-log


【解决方案1】:

您无法增加事件日志消息的大小,如果您尝试编写更长的消息,您应该会收到异常。

MSDN 表示如果您尝试记录消息的长度超过最大限制 31,839 字节(在 Windows Vista 之前的 Windows 操作系统上为 32,766 字节)ArgumentException 将被抛出。

【讨论】:

  • 消息远小于 31KB。
  • 消息长度约为 1.5KB,因此它看起来比您引用的限制不是限制因素。也许消息源是限制因素,但我不确定除了处理我的服务中的错误并手动写入事件日志之外是否可以更改它。
猜你喜欢
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 2017-09-08
  • 1970-01-01
相关资源
最近更新 更多