You are getting errors because your ASP.NET worker process does not have permission to create the new event source "Enterprise Library Logging" in the application event log.
You need to create the source once and then it will work. You can either:
NOTE: You must have admin permissions to create an event source.
2. Run the application as an admin using impersonation, and purposely cause an error to be thrown. Since you have admin permissions, and the web application is impersonating you, then the event source will be created.
This only needs to be done once per web server and then it will work. It takes a higher privilege to create an event source, but the ASP.NET worker process identities can write to them fine.
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
karang
Contributor
2441 Points
919 Posts
Unable to log into event log
Feb 21, 2012 08:49 AM|LINK
Hi
I have created a web.config file for logging the exceptions in enterprise library.
[code]
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Event Log Listener"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
source="Enterprise Library Logging" formatter="Text Formatter"
log="Application" machineName="." traceOutputOptions="LogicalOperationsStack" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Timestamp: {timestamp}{newline}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Thread Name: {threadName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
Extended Properties: {dictionary({key} - {value}{newline})}"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Event Log Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<exceptionHandling>
<exceptionPolicies>
<add name="MyPolicy">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None" />
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
<appSettings>
<add key="Source" value="Test" />
</appSettings>
[/code]
And when ever exception occurs I have written the following code
[code]
Exception ex = new Exception(message);
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, "MyPolicy");
[\code]
But I can't see the exception in the event log have I missed something?
Karan Gupta
http://gyansangrah.com
Please click "Mark as Answer" if this helped you.
DarrellNorto...
All-Star
86809 Points
9646 Posts
Moderator
MVP
Re: Unable to log into event log
Feb 21, 2012 10:29 AM|LINK
You are getting errors because your ASP.NET worker process does not have permission to create the new event source "Enterprise Library Logging" in the application event log.
You need to create the source once and then it will work. You can either:
1. Go in to the registry on the web server using regedit and add the event log source to the application event log. You can follow the directions here: http://msdn.microsoft.com/en-us/library/xz73e171(v=VS.90).aspx or use an app that someone built here: http://imar.spaanjaars.com/275/logging-errors-to-the-event-log-in-aspnet-applications
NOTE: You must have admin permissions to create an event source.
2. Run the application as an admin using impersonation, and purposely cause an error to be thrown. Since you have admin permissions, and the web application is impersonating you, then the event source will be created.
This only needs to be done once per web server and then it will work. It takes a higher privilege to create an event source, but the ASP.NET worker process identities can write to them fine.
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.