在 C# 中写入应用程序事件日志
Write to Application EventLog in C#
我想在事件查看器中为许多不同的事件编写自定义源。
我希望日志名称为“Application”,源名称为“DDG ServiceWare”。
我查看了 MSDN 上的文档并做了这个:
private static void _writeToApplicationEventLog(string logMessage, int eventID)
{
if(!EventLog.SourceExists("DDG ServiceWare"))
{
EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "Application");
EventLog.CreateEventSource(exitEvents);
}
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "DDG ServiceWare";
eventLog.WriteEntry(logMessage, EventLogEntryType.Error, eventID);
}
}
然而,当我 运行 它时,我得到一个例外:
'System.ArgumentException' 类型的未处理异常发生在 System.dll
源 'DDG ServiceWare' 未在日志 'Application' 中注册。 (在log 'DDG ServiceWare'中注册。) " Source和Log属性必须匹配,或者你可以将Log设置为空字符串,它会自动匹配到Source 属性.
即使我把EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "Application");
改成EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "DDG ServiceWare");
也没有改变
我做错了什么?
您可能需要考虑像 SeriLog 这样的软件包,它可以使日志记录变得更加容易(不仅是事件日志,还包括文件和其他目标)。看看这个https://github.com/serilog/serilog/wiki/Writing-Log-Events
我为你创建了例子
void Main()
{
_writeToApplicationEventLog("test", 123, EventLogEntryType.Error);
_writeToApplicationEventLog("test", 123, EventLogEntryType.Warning);
_writeToApplicationEventLog("test", 123, EventLogEntryType.Information);
}
private static void _writeToApplicationEventLog(string logMessage, int eventID, EventLogEntryType logType)
{
string sourceName = "SampleApplicationSource";
string myLogName = "myNewLog";
string messageFile = "customlog.log";
if (!EventLog.SourceExists(sourceName))
{
EventSourceCreationData mySourceData = new EventSourceCreationData(sourceName, myLogName);
mySourceData.MessageResourceFile = messageFile;
mySourceData.CategoryResourceFile = messageFile;
mySourceData.CategoryCount = 1;
mySourceData.ParameterResourceFile = messageFile;
EventLog.CreateEventSource(mySourceData);
}
myLogName = EventLog.LogNameFromSourceName(sourceName,".");
using (EventLog eventLog = new EventLog(myLogName, ".", sourceName))
{
eventLog.Source = sourceName;
eventLog.WriteEntry(logMessage, logType, eventID);
}
}
在名为 myNewLog 的应用程序和服务下创建的日志
我想在事件查看器中为许多不同的事件编写自定义源。
我希望日志名称为“Application”,源名称为“DDG ServiceWare”。 我查看了 MSDN 上的文档并做了这个:
private static void _writeToApplicationEventLog(string logMessage, int eventID)
{
if(!EventLog.SourceExists("DDG ServiceWare"))
{
EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "Application");
EventLog.CreateEventSource(exitEvents);
}
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "DDG ServiceWare";
eventLog.WriteEntry(logMessage, EventLogEntryType.Error, eventID);
}
}
然而,当我 运行 它时,我得到一个例外:
'System.ArgumentException' 类型的未处理异常发生在 System.dll 源 'DDG ServiceWare' 未在日志 'Application' 中注册。 (在log 'DDG ServiceWare'中注册。) " Source和Log属性必须匹配,或者你可以将Log设置为空字符串,它会自动匹配到Source 属性.
即使我把EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "Application");
改成EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "DDG ServiceWare");
也没有改变
我做错了什么?
您可能需要考虑像 SeriLog 这样的软件包,它可以使日志记录变得更加容易(不仅是事件日志,还包括文件和其他目标)。看看这个https://github.com/serilog/serilog/wiki/Writing-Log-Events
我为你创建了例子
void Main()
{
_writeToApplicationEventLog("test", 123, EventLogEntryType.Error);
_writeToApplicationEventLog("test", 123, EventLogEntryType.Warning);
_writeToApplicationEventLog("test", 123, EventLogEntryType.Information);
}
private static void _writeToApplicationEventLog(string logMessage, int eventID, EventLogEntryType logType)
{
string sourceName = "SampleApplicationSource";
string myLogName = "myNewLog";
string messageFile = "customlog.log";
if (!EventLog.SourceExists(sourceName))
{
EventSourceCreationData mySourceData = new EventSourceCreationData(sourceName, myLogName);
mySourceData.MessageResourceFile = messageFile;
mySourceData.CategoryResourceFile = messageFile;
mySourceData.CategoryCount = 1;
mySourceData.ParameterResourceFile = messageFile;
EventLog.CreateEventSource(mySourceData);
}
myLogName = EventLog.LogNameFromSourceName(sourceName,".");
using (EventLog eventLog = new EventLog(myLogName, ".", sourceName))
{
eventLog.Source = sourceName;
eventLog.WriteEntry(logMessage, logType, eventID);
}
}
在名为 myNewLog 的应用程序和服务下创建的日志