CodeMonkey1313
CodeMonkey1313

Reputation: 16011

Using XmlLogFormatter with the RollingFlatFileTraceListener in Enterprise Library

I'm trying to use the RollingFlatFileTraceListener to provide rolling logs in my app along side the XmlLogFormatter so that the logs are in an XML format, however the app no longer seems to be logging anything.

<listeners>
  <clear />
  <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="C:\Inetpub\logs\rolling.log" rollFileExistsBehavior="Increment" header="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" footer="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" formatter="Error Formatter"
        rollSizeKB="12499" maxArchivedFiles="3200" traceOutputOptions="None" timeStampPattern="yyyy-MM-dd" rollInterval="Midnight" />
</listeners>
<formatters>
  <clear />
  <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.XmlLogFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp(local)}&#xA;Message: {message}&#xA;Category: {category}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Extended Properties: {dictionary({key} - {value}&#xA;)}" name="Error Formatter" />
</formatters>

Since the application isn't hard faulting, I can't see any errors to diagnose, yet I know that I should see some trace logs by now.

Update:

Current progress is that I've been able to use the RollingFlatFileTraceListnerData with the TextFormatter, making use of the template to specify XML. The two items that don't work as of yet are

Any thoughts on how to tack that on to the start and end of the file?

Upvotes: 1

Views: 1403

Answers (1)

Randy Levy
Randy Levy

Reputation: 22655

The out of the box trace listeners do not support a file header or file footer concept. As you've seen, it basically just appends to the file. Even if you used the .NET Framework System.Diagnostics.XmlWriterTraceListener it only writes XML fragments and not well formed XML documents.

One way to achieve what you want would be to create a separate process that modifies the archived files to be well formed after they have been rolled. The downside of that approach is that the active log file is not well formed.

If that is a concern then you will probably have to create a custom trace listener to do what you want. Instead of simply appending to the log file you could overwrite the XML document's closing tag (e.g. ) with the latest LogEntry and the closing tag.

Another interesting approach from the article Efficient Techniques for Modifying Large XML Files is to create a well formed XML document that includes the document with XML fragments. E.g.

 <?xml version="1.0"?>
  <!DOCTYPE logfile [
  <!ENTITY events    
   SYSTEM "XMLFile1.xml">
  ]>
  <logfile>
    &events;
  </logfile>

Upvotes: 1

Related Questions