EDN Admin
Well-known member
hello
I am using Enterprise library for logging and try to write the exception in a log file / text file.<br/>
What I did is I have copied required for Enterprise Library to my machine from a different machine (working project).
C# Code - <br/>
public static void LogError(Exception ex, string additionalMessage)<br/>
{<br/>
LogEntry logEntry = new LogEntry();
logEntry.Categories.Add("MedicalCongressErrors");<br/>
logEntry.Severity = TraceEventType.Error;<br/>
logEntry.Message = ex.Message;<br/>
if (!string.IsNullOrEmpty(ex.StackTrace))<br/>
{<br/>
logEntry.ExtendedProperties.Add("StackTrace:", ex.StackTrace);<br/>
}<br/>
logEntry.ExtendedProperties.Add("Additional Info:", additionalMessage);<br/>
logEntry.ExtendedProperties.Add("filelocation", @"C:LogsTest1.txt"); //newly Added .. can be COMMENTED as it there in web.config
<br/>
Logger.Write(logEntry);<br/>
}<br/>
Configured web.config -
<br/>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true <br/>
<listeners><br/>
<add fileName="C:LogsDashboardtrace.log" header="" footer="" formatter="DashboardTextFormatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging,
Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="FlatFile TraceListener"/><br/>
<add source="Enterprise Library Logging" 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"/><br/>
</listeners><br/>
<formatters><br/>
<add template="Timestamp: {timestamp}
Message: {message}
EventId: {eventid}
Severity: {severity}
Machine: {machine}
Application Domain: {appDomain}
Extended Properties: {dictionary({key}
- {value}
}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="DashboardTextFormatter"/><br/>
<add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application
Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter"/><br/>
</formatters><br/>
<logFilters><br/>
<add categoryFilterMode="DenyAllExceptAllowed" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Category Filter <br/>
<categoryFilters><br/>
<add name="DashboardErrors"/><br/>
</categoryFilters><br/>
</add><br/>
</logFilters><br/>
<categorySources><br/>
<add switchValue="Error" name="DashboardErrors <br/>
<listeners><br/>
<add name="FlatFile TraceListener"/><br/>
</listeners><br/>
</add><br/>
<add switchValue="All" name="General <br/>
<listeners><br/>
<add name="Formatted EventLog TraceListener"/><br/>
</listeners><br/>
</add><br/>
</categorySources><br/>
<specialSources><br/>
<allEvents switchValue="All" name="All Events"/><br/>
<notProcessed switchValue="All" name="Unprocessed Category"/><br/>
<errors switchValue="All" name="Logging Errors & Warnings <br/>
<listeners><br/>
<add name="Formatted EventLog TraceListener"/><br/>
</listeners><br/>
</errors><br/>
</specialSources><br/>
</loggingConfiguration>
Please HELP me in achieving this ... I dont think I am missing anything ...
Thanks<br/>
Santanu
View the full article
I am using Enterprise library for logging and try to write the exception in a log file / text file.<br/>
What I did is I have copied required for Enterprise Library to my machine from a different machine (working project).
C# Code - <br/>
public static void LogError(Exception ex, string additionalMessage)<br/>
{<br/>
LogEntry logEntry = new LogEntry();
logEntry.Categories.Add("MedicalCongressErrors");<br/>
logEntry.Severity = TraceEventType.Error;<br/>
logEntry.Message = ex.Message;<br/>
if (!string.IsNullOrEmpty(ex.StackTrace))<br/>
{<br/>
logEntry.ExtendedProperties.Add("StackTrace:", ex.StackTrace);<br/>
}<br/>
logEntry.ExtendedProperties.Add("Additional Info:", additionalMessage);<br/>
logEntry.ExtendedProperties.Add("filelocation", @"C:LogsTest1.txt"); //newly Added .. can be COMMENTED as it there in web.config
<br/>
Logger.Write(logEntry);<br/>
}<br/>
Configured web.config -
<br/>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true <br/>
<listeners><br/>
<add fileName="C:LogsDashboardtrace.log" header="" footer="" formatter="DashboardTextFormatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging,
Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="FlatFile TraceListener"/><br/>
<add source="Enterprise Library Logging" 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"/><br/>
</listeners><br/>
<formatters><br/>
<add template="Timestamp: {timestamp}
Message: {message}
EventId: {eventid}
Severity: {severity}
Machine: {machine}
Application Domain: {appDomain}
Extended Properties: {dictionary({key}
- {value}
}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="DashboardTextFormatter"/><br/>
<add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application
Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter"/><br/>
</formatters><br/>
<logFilters><br/>
<add categoryFilterMode="DenyAllExceptAllowed" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Category Filter <br/>
<categoryFilters><br/>
<add name="DashboardErrors"/><br/>
</categoryFilters><br/>
</add><br/>
</logFilters><br/>
<categorySources><br/>
<add switchValue="Error" name="DashboardErrors <br/>
<listeners><br/>
<add name="FlatFile TraceListener"/><br/>
</listeners><br/>
</add><br/>
<add switchValue="All" name="General <br/>
<listeners><br/>
<add name="Formatted EventLog TraceListener"/><br/>
</listeners><br/>
</add><br/>
</categorySources><br/>
<specialSources><br/>
<allEvents switchValue="All" name="All Events"/><br/>
<notProcessed switchValue="All" name="Unprocessed Category"/><br/>
<errors switchValue="All" name="Logging Errors & Warnings <br/>
<listeners><br/>
<add name="Formatted EventLog TraceListener"/><br/>
</listeners><br/>
</errors><br/>
</specialSources><br/>
</loggingConfiguration>
Please HELP me in achieving this ... I dont think I am missing anything ...
Thanks<br/>
Santanu
View the full article