locked
need to write a log file with EnterpriseLibrary RRS feed

  • Question

  • User-1851892399 posted

    Hi, 

    I need to write log file with the help of EnterpriseLibrary.

    below code write "Start Process" in log file and after log file get locked. so End Process write in another file

    My code is as below :

    protected void Page_Load(object sender, EventArgs e)
        {
    
            Loggers("Start Process","");
    
            DateTime date1 = DateTime.Now;
    
            DateTime dtUTC = TimeZone.CurrentTimeZone.ToUniversalTime(date1.AddDays(-5));
    
            DateTime dtLocalTime = TimeZone.CurrentTimeZone.ToLocalTime(dtUTC);
    
            Loggers("End Process","");
    
            Response.Write(" Universal time2 -- dtUTC " + dtUTC);
            Response.Write(" LocalTime -- dtLocalTime " + dtLocalTime);
        }
    
    public void Loggers(string message,string categroy)
        {
            
    
            var factory = new LogWriterFactory();
            var logwriter = factory.Create();
            
    
            //if (logwriter.IsLoggingEnabled())
            //{
                logwriter.Write(message);
            //}
        }
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
          <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              source="FTS Web" formatter="Text Formatter"
              log="" machineName="." traceOutputOptions="None" />
          <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              fileName="trace.log" formatter="Text Formatter" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
        </listeners>
        <formatters>
          <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.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="Event Log Listener" />
              <add name="Flat File Trace Listener" />
            </listeners>
          </add>
        </categorySources>
        <specialSources>
          <allEvents switchValue="All" name="All Events">
            <listeners>
              <add name="Flat File Trace Listener" />
            </listeners>
          </allEvents>
          <notProcessed switchValue="All" name="Unprocessed Category">
            <listeners>
              <add name="Flat File Trace Listener" />
            </listeners>
          </notProcessed>
          <errors switchValue="All" name="Logging Errors &amp; Warnings">
            <listeners>
              <add name="Flat File Trace Listener" />
            </listeners>
          </errors>
        </specialSources>
      </loggingConfiguration>



    Friday, September 19, 2014 1:25 AM

All replies

  • User-484054684 posted

    I think, creating the instance each and everytime for single file is creating the problem.

    var factory = new LogWriterFactory();
           
    var logwriter = factory.Create();

    The above two lines look causing creating a trace listener everytime. So try to use single instance and start writing into the file.

    Reference: http://entlib.codeplex.com/discussions/474889

     

    Friday, September 19, 2014 8:00 AM
  • User281708951 posted

    Thank you for this post. Had same issue, and creating the instance every time for single file was the issue. Rewrite the Loggers method as below

    public void Loggers(string message,string categroy)
        {
            var factory = new LogWriterFactory();
            using (var logwriter = factory.Create()){
            logwriter.Write(message);
            }
        }
    //or explicitly call logwriter.Dispose();

    Sunday, November 2, 2014 6:20 AM