save exception to log file Using EnterPrise Library ?????

Topics: Exception Handling Application Block, Logging Application Block
Feb 5, 2010 at 1:11 PM
Edited Feb 5, 2010 at 1:15 PM

I am working over to save exception to log file Using EnterPrise Library In website and WCF

Used following code  ,giving  no error run successfully

But I am unable to find where this log enrty is made(path) .I  also checked system log file.

Does  we need to give the log file destination path (Then what is the syntax???)

I have also to use Email tracelistener,make entry to database using Library(Any useful example ,link most welcome)

my code sample:

LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = "Informational message";
logEntry.Categories.Add("UI Events");

In My webconfig i used following Setting:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
      <add toAddress="" fromAddress=""
        subjectLineStarter="" subjectLineEnder="" smtpServer=""
        smtpPort="given port no here" formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Email" />
      <add source="Enterprise Library Logging" formatter="Text Formatter"
        log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Formatted EventLog TraceListener" />
      <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=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
      <add switchValue="All" name="General">
          <add name="Formatted EventLog TraceListener" />
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
          <add name="Formatted EventLog TraceListener" />



Feb 8, 2010 at 2:10 AM

You didn't have any categories defined in your config file that is named either "Trace" or "UIEvents".  You only have a "General" category and if you change you logEntry object to add the "General" string in the Categories property instead of adding "Trace" and "UIEvents", it will log in the Event Viewer under the Application Log.  But if you want to log to a file, you need to add a Flat File Trace Listener under the Trace Listeners node.  It has a FileName property which can be set to either a relative or a physical path.  Next, you need to reference that flat file trace listener from a category.  You can either add it in the General category or create a new one for it.  If you add it in the General category, your exception will be logged to both the Event Viewer and in the file you specified in the FileName property.  Same goes for logging using Email Trace Listener.  For the properties which you need to set for an email trace listener, please refer to the documentation -

By the way, you can automatically log exceptions without creating a log entry object by using the Exception Handling Block -


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.