Enterprise Library Logging not logging to Event Log from ASP.NET

前端 未结 4 2071
清歌不尽
清歌不尽 2021-02-19 23:16

I spent a day trying to make Ent Lib Logging work and log anything into database or event log. I have a web application and console application with the same Ent Lib config but

相关标签:
4条回答
  • 2021-02-19 23:38

    run visual studio as administrator

    1. Right click visual studio icon
    2. Click "Run as administrator"
    0 讨论(0)
  • 2021-02-19 23:45

    or use this code....

     public static void RunSnippet(string logName, string eventSource)
    {
        // this will throw an exception if you don't have admin rights
        if (!EventLog.SourceExists(eventSource))
        {
            System.Diagnostics.EventLog.CreateEventSource(eventSource, logName);
            Console.WriteLine("Event Log and Source: {0},{1} were created successfully.",logName, eventSource);
        }
        else
        {
            Console.WriteLine("Event log/source: {0}/{1} already exists",logName, eventSource);
        }
        Console.WriteLine("Done");
    
    }
    
    0 讨论(0)
  • 2021-02-19 23:55

    I use a PowerShell script to create the appropriate source ...

    $source = "FoToIaW"
    if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) 
    {
      [System.Diagnostics.EventLog]::CreateEventSource($source, "Application")
    }
    
    0 讨论(0)
  • 2021-02-20 00:01

    I believe that under IIS7 (which I am assuming you are using) the application pool will be running under NETWORK SERVICE.

    You could try giving NETWORK SERVICE Full Control to the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application.
    (Not Recommended!)

    Alternatively, you could grant EVERYONE Full control to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application, log a message, then revert that change. Once the key is set up then you won't need the permissions to write to the registry.

    Or you could manually configure the registry keys that you require beforehand to avoid the permission problems:

    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\Logger]
    "EventMessageFile"="c:\\WINNT\\Microsoft.NET\\Framework\\v2.0.50727\\EventLogMessages.dll"
    


    Just an update to this answer that according to MSDN: "To create an event source in Windows Vista and later or Windows Server 2003, you must have administrative privileges".

    0 讨论(0)
提交回复
热议问题