0

Trace File Path in App.Config as follows -

<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
      <listeners>
         <add fileName="D:\trace.log" header="----------------------------------------" footer="----------------------------------------" formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="FileTraceListener" />
      </listeners>
      <formatters>
         <add 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})}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter" />
      </formatters>
      <categorySources>
         <add switchValue="All" name="General">
            <listeners>
               <add name="FileTraceListener" />
            </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="FileTraceListener" />
            </listeners>
         </errors>
      </specialSources>
</loggingConfiguration>

Here we have defined one listener i.e. FileTraceListener. Please help me to get following answers -

  1. How to access the trace path?
  2. How to write details into trace file?
Rup
  • 33,765
  • 9
  • 83
  • 112
Sumit Deshpande
  • 2,155
  • 2
  • 20
  • 36

1 Answers1

2

You are using Enterprise Library Logging, so to get the first logger you would use this code:

LoggingSettings loggingSettings = (LoggingSettings)ConfigurationManager.GetSection(LoggingSettings.SectionName);
TraceListenerData traceListenerData = loggingSettings.TraceListeners.Get(0);
FlatFileTraceListenerData objFlatFileTraceListenerData = traceListenerData as FlatFileTraceListenerData;

string logFilePath = objFlatFileTraceListenerData.FileName; 

Note: If you want to get a specific listener replace Get(0) with Get("FlatFile TraceListener"); etc.

Belogix
  • 8,129
  • 1
  • 27
  • 32
  • I think he specifically wants the values from `` rather than duplicating the path as a new setting. – Rup May 09 '13 at 11:09
  • 1
    Here the requirement is to access trace path which is defined into app.config file in the format metioned above without duplicating into another variable. – Sumit Deshpande May 09 '13 at 11:10
  • Above link is not useful in this situation as help provided into this link is how to change the path of log file defined into Enterprise Library. Here we don't want to change the path. – Sumit Deshpande May 09 '13 at 11:23
  • 1
    Setting / Getting is same principle?! To make life easier I have updated my answer with the exact code snippet you are after. – Belogix May 09 '13 at 11:30