Accessing and Maintaining Log Files

As we all know there are three ways to maintain the logs:

  • In Log file in Text document
  • In Event log of your PC
  • In Database

How to retrieve a value from the Log file

Var path= AppDomain.CurrentDomain.BaseDirectory + "\\" + logFileName

Using "AppDomain.CurrentDomain.BaseDirectory" you get your log file relative path and then you just need to add your log file name.

How to retrieve a value from the Event Log Viewer

First we need to make an object of the listener ("EventLogTraceListener"). We need to pass the name of the folder you made in the Event Viewer to track the log.

EventLogTraceListener myTraceListener = new EventLogTraceListener("NameofFolder");

We will get a collection of entries that we stored using "EventLogEntryCollection".

EventLogEntryCollection coll = myTraceListener.EventLog.Entries;


After having all the information in the coll object of "EventLogEntryCollection" we can separate each log and check the message. For that we need to make an "EventLogEntry" object and using foreach we can check it.

foreach (EventLogEntry entry in coll)

{

   bool ex = entry.Message.Contains(exception1);

}

How to Configure the log

<configSections>

    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.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.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

        source="Enterprise Library Logging" formatter="Text Formatter"

        log="NameofFolder" machineName="." traceOutputOptions="None" />
      <
add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
       
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
"
       
fileName="Name.trace.log" formatter="Text Formatter" />
    </
listeners>
    <
formatters>
      <
add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      
template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority:
{priority}{newline}
&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine
{newline}
&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name:
{localProcessName}{newline}
&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended
Properties: {dictionary({key} - {value}{newline})}
"
       
name="Text Formatter" />
    </
formatters>
    <
categorySources>
      <
add switchValue="All" name="General">
        <
listeners>
          <
add name="Flat File Trace Listener" />
          <
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 &amp; Warnings">
        <
listeners>
          <
add name="Flat File Trace Listener" />
          <
add name="Event Log Listener"/>
        </
listeners>
      </
errors>
    </
specialSources>
  </
loggingConfiguration>




NOTE: Above code is for Microsoft Enterprise Library.

What is Microsoft Enterprise library?

Set of of classes provided by Microsoft with help of that you can do  multiple things:

  • Exception Handling
  • Logging
  • Caching
  • Security



Similar Articles