6

In my web application I am trying to create a log file for logging errors and exceptions, but when I run my application, log file is not getting created in my solution folder or in bin folder.

I used the following code. Please help me I am stuck up with this problem, thank you in advance.

namespaces used

using Microsoft.Practices.EnterpriseLibrary.Logging;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

.cs file

        public int GetdeskKMasterRecordsCount(string CircleId, string StoreId)
        {
            try
            {
               throw new Exception("this is normal exception");

            }
            catch (Exception ex)
            {
                BindLog(ex);
                return 0;
            }
        }


        public static void BindLog(Exception ex)
        {
            if (ex == null) return;
            Logger.Write(LogInformation(1, DateTime.Now, ex.Message, " "));

        }

        public static LogEntry LogInformation(int eventId, DateTime timeStamp, string message, string documentName)
        {
            LogEntry logEntryObject = new LogEntry();
            logEntryObject.EventId = eventId;
            logEntryObject.Title = documentName;
            logEntryObject.TimeStamp = timeStamp;
            logEntryObject.MachineName = System.Environment.MachineName;
            logEntryObject.Message = message;
            logEntryObject.Categories.Add("Exception");

            return logEntryObject;
        }

Web Config file

<configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <section name="recordingWindowGroup" type="Vodafone.DMS.BAL.WindowConfigurationHandler, Vodafone.DMS.BAL"/>
    <section name="defaultParamGroup" type="Vodafone.DMS.BAL.DefaultParamConfiguration, Vodafone.DMS.BAL"/>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add name="Rolling Flat File Trace Listener"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           fileName="Vodafone.DMS.log.exclude" footer="---------------------------" formatter="Text Formatter" header="---------------------------" rollFileExistsBehavior="Increment"
           rollSizeKB="10" timeStampPattern="yyyy-MM-dd hh:mm:ss" maxArchivedFiles="7" traceOutputOptions="Timestamp, Callstack" filter="All"/>
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;"
           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="Rolling Flat File Trace Listener"/>
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Rolling Flat File Trace Listener"/>
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Rolling Flat File Trace Listener"/>
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Rolling Flat File Trace Listener"/>
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
sree
  • 601
  • 5
  • 12
  • 33

3 Answers3

4

For enterprise library version 5.0 and above, Microsoft has released an enterprise library configuration tool that allows you to change the configurations visually in the configuration file. You can also install this tool from NuGet and that can be accessed from inside the visual studio Tools >> Library Package Manager >> Manage NuGet Packages for solution and add the appropriate EntLib Application Block. You can find guide from here.

Using that tool I have generated a configuration file which is as follows:

Place this in Web.Config or App.Config file

  <loggingConfiguration name="loggingConfiguration" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="RollingFlatFile.log"
        footer="---------------------------" formatter="Text Formatter"
        header="---------------------------" rollFileExistsBehavior="Increment"
        rollInterval="Week" rollSizeKB="20000" timeStampPattern="yyyy-MM-dd hh:mm:ss"
        maxArchivedFiles="7" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack"
        filter="All" />
    </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}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

I would recommend that instead of generating the log file inside the bin directory of the application you should specify a physical location by changing the value of the fileName="RollingFlatFile.log" to something like fileName="c:\logs\RollingFlatFile.log"

Arafat
  • 1,390
  • 2
  • 12
  • 18
0

You need to set a category on your LogEntry to match a category source in your configuration file ('General' in the configuration you posted). I would also suggest looking at the Exception Handling Application Block if you are planning to log exceptions, instead of writing the code to handle creating a LogEntry yourself.

Tim B
  • 2,340
  • 15
  • 21
-2
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create(), false);
Logger.Write("btnStartProcess_Click");
Logger.Reset();
royhowie
  • 11,075
  • 14
  • 50
  • 67
  • Generally you want to post more than a code dump. Explain why this'll work/etc. You'll help both the OP and future readers by doing so. – royhowie Nov 24 '14 at 06:34