【问题标题】:C#_EventLog Exception [duplicate]C#_EventLog 异常 [重复]
【发布时间】:2018-08-09 08:10:02
【问题描述】:

我正在尝试编写一个简单的事件日志,但遇到“System.Security.SecurityException:找不到源...” 我搜索了很多,但找不到有效的解决方案,如果有人帮助我,我真的很感激???????? 我知道需要创建源并且必须注册密钥,但是密钥是什么? 我该怎么做?

String source =“DemoTestApplication”;
String log = “DemoEventLog”;
EventLog demolog=new EventLog(log);
EventLog demolog=new EventLog(log);
demolog.Source=source;
demolog.writeEntry(“This is the first message to the log”,EventLogEntryType.Information);

【问题讨论】:

    标签: c# event-log eventlog-source custom-eventlog


    【解决方案1】:

    试试这个:

    public static void LogEvent(string logBuffer, EventLogEntryType eventLogEntryType)
    {
        const string source = "DemoTestApplication";
    
        // The user may not have permissions to access any event logs, therefore catch any SecurityExceptions.
        try
        {
            if (!EventLog.SourceExists(source))
            {
                // This requires administrator privileges.
                EventLog.CreateEventSource(source, "Application");
            }
            using (EventLog eventLog = new EventLog())
            {
                eventLog.Source = source;
                eventLog.WriteEntry(logBuffer, eventLogEntryType);
            }
        }
        catch (System.Security.SecurityException ex)
        {
            // Source was not found, but some or all of the event logs could not be searched.
            // May occur e.g., when trying to search Security event log.
            // EventLog.CreateEventSource requires permission to read all event logs to make sure 
            // that the new source name is unique.
            Debug.WriteLine(logBuffer);
            Debug.WriteLine(ex.ToString());
        }
    }
    

    通常最好在以管理员/提升权限运行时创建事件日志源,例如在安装软件期间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 2020-11-14
      • 2012-03-29
      • 2010-12-18
      相关资源
      最近更新 更多