C#_EventLog Exception

Я пытаюсь написать простой журнал событий, но сталкиваюсь с "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);

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());
    }
}

Обычно лучше создавать источник журнала событий при работе с правами администратора или с повышенными правами, например, во время установки программного обеспечения.

Другие вопросы по тегам