gpt4 book ai didi

c# - EventLog WriteEntry 不写入指定日志,而是写入应用程序日志

转载 作者:太空狗 更新时间:2023-10-29 23:25:29 25 4
gpt4 key购买 nike

我有一个应用程序,我想在其中将条目写入事件日志记录器通过 MEF 实例化。我创建了一个派生类,以便能够在使用它之前执行日志初始化。

我的代码如下:

public class WinEventLog : EventLog, ILogger
{
private const string LOG_SourceName = "DataGen_Source";
private const string LOG_SysLogName = "Pool_Log";

private bool _isInitialized = false;

public WinEventLog()
: base()
{
Initialize();
}

public void LogMessage(MessageLevel level, string message)
{
WriteEntry(message, level.EventLogType());
}

public void LogMessage(string source, MessageLevel level, string message)
{
WriteEntry(source, message, level.EventLogType());
}

public void Initialize()
{
if (!_isInitialized)
{
this.BeginInit();
this.EndInit();

if (!System.Diagnostics.EventLog.SourceExists(LOG_SourceName))
{
System.Diagnostics.EventLog.CreateEventSource(
LOG_SourceName, LOG_SysLogName);
}
Source = LOG_SourceName;
Log = LOG_SysLogName;
_isInitialized = true;
}
}
}

但是,logger 并没有写入我指定的日志 Pool_Log,而是写入了 Applications 日志。

知道为什么会这样吗?


编辑

我从其他项目中引用了完全相同的组件,在这种情况下它写入了正确的 EventLog !!!

我很困惑!

谢谢

最佳答案

看来您正在创建源代码,但没有创建实际的日志,例如,如果我想创建一个 SQLEventLog,我会执行类似以下的操作

public bool CreateLog(string strLogName)
{
bool Result = false;

try
{
System.Diagnostics.EventLog.CreateEventSource(strLogName, strLogName);
System.Diagnostics.EventLog SQLEventLog =
new System.Diagnostics.EventLog();

SQLEventLog.Source = strLogName;
SQLEventLog.Log = strLogName;

SQLEventLog.Source = strLogName;
SQLEventLog.WriteEntry("The " + strLogName + " was successfully
initialize component.", EventLogEntryType.Information);

Result = true;
}
catch
{
Result = false;
}

return Result;
}

让我知道这是否有帮助.. 看起来您缺少事件日志创建

关于c# - EventLog WriteEntry 不写入指定日志,而是写入应用程序日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9364258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com