How to write to the Windows event log?

I am trying to configure basic logging for Windows.net through the event log System.Diagnostics.EventLog, but I fail to see any events that are actually logged. Consider the following code:

// Elsewhere in the class
private static readonly string EventLogName = "LogName";
private static readonly string EventLogSource = "AppName";

// In the only function that does something
if (!EventLog.Exists(EventLogName))
{
    EventLog.CreateEventSource(EventLogSource, EventLogName);
    return;
}
else
{
    Trace.TraceInformation("Attempting log");

    // This doesn't write anything
    EventLog.WriteEntry(EventLogSource, 
        "StaticWriteEntry", 
        EventLogEntryType.Error);

    // Neither does this
    using (var log = new EventLog())
    {
        log.Log = EventLogName;
        log.Source = EventLogSource;
        log.WriteEntry("WriteEntry?", EventLogEntryType.Error);
    }
}
return;

The first time I create a log and exit the application, modeled on MSDN. Of course, this log creation will ultimately go into customization. Subsequent runs attempt to write a message to the generated event log.

No exceptions are thrown. The log was created successfully (I can open it in the Windows Event Viewer). Nothing relevant is logged in the security log.

WriteEntry(). - . Server2008 UAC. ( , .) ?

( , EventLogTraceListener app.config, -, .)

+3
1

, , . , , . - EventLog.Source docs:

, , .

, , , .

, "OldSource" "OldLog". , , , "NewSource" "NewLog". , "NewLog". "NewLog", "OldSource", .

, , ( "NewLog" "OldSource"), .

, , !:)

+4

Source: https://habr.com/ru/post/1707368/


All Articles