【发布时间】:2015-08-26 19:35:04
【问题描述】:
我想将事件日志跟踪添加到我的 winforms 应用程序,但对此不太了解。
我所做的是以下步骤: 在我的 app.config 中:
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.EventLogTraceListener" initializeData="MyApplication"/>
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
-
这样的基本记录器类:
使用系统; 使用 System.Diagnostics;
命名空间 LogEventSample { 公共静态类 Logger { 公共静态无效错误(字符串消息,字符串模块) { WriteEntry(消息,“错误”,模块); }
public static void Error(Exception ex, string module) { WriteEntry(ex.Message, "error", module); } public static void Warning(string message, string module) { WriteEntry(message, "warning", module); } public static void Info(string message, string module) { WriteEntry(message, "info", module); } private static void WriteEntry(string message, string type, string module) { Trace.WriteLine(string.Format("{0},{1},{2},{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), type, module, message)); } }}
然后,当我需要这样的一行时:
Logger.Info("working...", "MyApp");
我的疑虑来自应用程序的生命周期。 这会影响我的应用程序性能吗?我应该用不同的方式做这个吗?
另外,当我登录时如何存储更多信息?我不谈论存储一些异常字符串,而是保存更详细的数据以显示在事件查看器中。
【问题讨论】: