*重要提示:本帖旨在解决您问题的根本原因,而不是提供您特别要求的解决方案。我意识到这篇文章已经过时,但仍然觉得有必要做出贡献。*
我的团队曾遇到类似问题,我们改变了日志管理方式,显著减少了生产支持和错误修补时间。从实用角度来看,这适用于我们团队大多数企业应用程序:
- 使用“类名”。“函数名”前缀记录日志消息。
- 对于真正的错误,将捕获的异常输出到事件记录器中。
- 注重清晰的消息作为同行代码审查的一部分,而不是事件ID。
- 为每个函数使用唯一的事件ID,只需从上到下进行键入。
- 当编写每个函数的不同事件ID变得不切实际时,每个类应该只有一个唯一的事件ID(无论是否发生冲突)。
- 在过滤日志时,利用事件类别减少事件ID的依赖性。
当然,这取决于您的应用程序有多大以及数据的敏感程度。我们大多数应用程序都有10k到500k行代码,包含最少量的敏感信息。这可能看起来过于简单化,但从KISS的角度来看,它实用。
话虽如此,使用抽象的事件日志类来简化流程会使它容易使用,尽管清理可能不太愉快。例如:
MyClass.cs(使用包装器)
class MyClass
private bool MyFunction() catch (Exception oException)
return result;
}
}
EventLogAdapter.cs
class EventLogAdapter
{
private string _EventProgram = "";
private string _EventSource = "";
private string _ProgramName = "";
private string _ProgramVersion = "";
private string _EventClass = "";
private string _EventMethod = "";
private int _EventCode = 1;
private bool _Initialized = false;
private System.Diagnostics.EventLog oEventLog = new EventLog();
public EventLogAdapter() { }
public EventLogAdapter(
string EventProgram
, string EventSource
, string ProgramName
, string ProgramVersion
, string EventClass
) {
this.SetEventProgram(EventProgram);
this.SetEventSource(EventSource);
this.SetProgramName(ProgramName);
this.SetProgramVersion(ProgramVersion);
this.SetEventClass(EventClass);
this.InitializeEventLog();
}
public void InitializeEventLog() {
try {
if(
!String.IsNullOrEmpty(this._EventSource)
&& !String.IsNullOrEmpty(this._EventProgram)
){
if (!System.Diagnostics.EventLog.SourceExists(this._EventSource)) {
System.Diagnostics.EventLog.CreateEventSource(
this._EventSource
, this._EventProgram
);
}
this.oEventLog.Source = this._EventSource;
this.oEventLog.Log = this._EventProgram;
this._Initialized = true;
}
} catch { }
}
public void WriteEntry(string Message, System.Diagnostics.EventLogEntryType EventEntryType) {
try {
string _message =
"[" + this._ProgramName + " " + this._ProgramVersion + "]"
+ "." + this._EventClass + "." + this._EventMethod + "():\n"
+ Message;
this.oEventLog.WriteEntry(
Message
, EventEntryType
, this._EventCode
);
} catch { }
}
public void SetMethodInformation(
string EventMethod
,int EventCode
) {
this.SetEventMethod(EventMethod);
this.SetEventCode(EventCode);
}
public string GetEventProgram() { return this._EventProgram; }
public string GetEventSource() { return this._EventSource; }
public string GetProgramName() { return this._ProgramName; }
public string GetProgramVersion() { return this._ProgramVersion; }
public string GetEventClass() { return this._EventClass; }
public string GetEventMethod() { return this._EventMethod; }
public int GetEventCode() { return this._EventCode; }
public void SetEventProgram(string EventProgram) { this._EventProgram = EventProgram; }
public void SetEventSource(string EventSource) { this._EventSource = EventSource; }
public void SetProgramName(string ProgramName) { this._ProgramName = ProgramName; }
public void SetProgramVersion(string ProgramVersion) { this._ProgramVersion = ProgramVersion; }
public void SetEventClass(string EventClass) { this._EventClass = EventClass; }
public void SetEventMethod(string EventMethod) { this._EventMethod = EventMethod; }
public void SetEventCode(int EventCode) { this._EventCode = EventCode; }
}