12

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 only the console application is capable to log into the Event Log. I tried everything with permissions but I don't know what exactly I am doing — which services should have what. It does not work!

I read articles like this http://imar.spaanjaars.com/275/logging-errors-to-the-event-log-in-aspnet-applications and I want to try to give the ASPNET account those permissions. I am using Windows 7 and I cannot find ASPNET user account. So where is it?

This is the config file which is automatically generated from Ent Lib utility and it works only on App.config, not on web.config

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"
    revertImpersonation="false">
    <listeners>
      <add source="Logger" formatter="Text Formatter" log="Application"
        machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Formatted EventLog TraceListener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
Randy Levy
  • 22,566
  • 4
  • 68
  • 94
Costa
  • 3,897
  • 13
  • 48
  • 81
  • Can you post the relevant parts of your config? Are the WebApp and the ConsoleApp running on the same machine? – RoelF Jun 03 '10 at 12:30
  • yes, WebApp and ConsolApp run on the same machine – Costa Jun 03 '10 at 12:45
  • "I tried everything with permissions" -- what did you try? – Randy Levy Jun 03 '10 at 15:51
  • This is why I've ran away in terror from EntLib logging. I use Elmah for unhandled exception catching and a very simple sql server auditing widget using Linq-2-Sql that just lets you write a string with some metadata to log business events. – Chris Marisic Jan 12 '11 at 19:16

4 Answers4

4

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".

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • 2
    I think it is a bad idea to give network service full access to Registry. – Costa Jun 17 '10 at 08:08
  • Yes. I agree with you -- not recommended. – Randy Levy Jun 17 '10 at 18:40
  • 1
    You can give the NETWORK SERVICE account write privileges to only the event source you need. Tuzo mentions most of it above, but here's a document that might help: I know this is an older question now, but in order to use the event log, the event source must be created first (by a user having Administrative Privileges). In addition, if you are trying to use this within a web site, the NETWORK SERVICE account must be granted access to write to the event log. This site will help: http://msdn.microsoft.com/en-us/library/ms998320.aspx – Mark Aug 01 '12 at 14:59
4

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")
}
SteveC
  • 15,808
  • 23
  • 102
  • 173
2

run visual studio as administrator

  1. Right click visual studio icon
  2. Click "Run as administrator"
jflaga
  • 4,610
  • 2
  • 24
  • 20
1

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");

}
JoeBrockhaus
  • 2,745
  • 2
  • 40
  • 64
Raj Rao
  • 8,872
  • 12
  • 69
  • 83