Custom trace listener using enterprise applications

The project I'm currently working on uses the Enterprise Libraries V3.1 environment for journaling.

I need to take the log file that generated and archived it at specific points. It seems that the files built into Trace Listeners are stored in the intervals between registration events. I set up a custom Trace Listener that will attach to the file and close it so the file is always portable.

It seems like this (minus error handling for clarity):

[ConfigurationElementType(typeof(CustomTraceListenerData))] public class AlwaysClosedTextFileTraceListener : CustomTraceListener { private string logFilePath; public AlwaysClosedTextFileTraceListener () { logFilePath = @"hardcodedpath\log.txt"; } public override void Write(string message) { using (StreamWriter logFile = File.AppendText(logFilePath)) { logFile.Write(message); logFile.Flush(); logFile.Close(); } } public override void WriteLine(string message) { using (StreamWriter logFile = File.AppendText(logFilePath)) { logFile.WriteLine(message); logFile.Flush(); } } public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) { if (data is LogEntry && this.Formatter != null) { WriteLine(this.Formatter.Format(data as LogEntry)); } else { WriteLine(data.ToString()); } } } 

This works fine, but I'd rather pass the path as a parameter, rather than hardcoding.

For fun, I tried adding it to the constructor to find out what would happen:

  public LogFolderTraceListener(string logFilePath) { this.logFilePath = logFilePath; } 

When I do this, I get an error message indicating that I am doing wrong:

 System.InvalidOperationException : The type 'AlwaysClosedTextFileTraceListener' specified for custom trace listener named 'MyLogFile' does not a default constructor, which is required when no InitData is specified in the configuration. 

From that moment on, my research has changed a lot, the opposite of dead ends, endless problems with probability.

I found this to be scrolling through the source code for the built-in RollingTraceListener

  • There is a RollingFlatFileTraceListenerData : TraceListenerData class that seems to contain all the parameters passed to the constructor
  • At the bottom of the file for RollingFlatFileTraceListenerData is the RollingTraceListenerAssembler : TraceListenerAsssembler class, which looks like a factory
  • There is another SystemDiagnosticsTraceListenerNode : TraceListenerNode class SystemDiagnosticsTraceListenerNode : TraceListenerNode , which appears to make the Data class presentable to the configuration application

My question is this: how to create a CustomTraceListener with a custom path parameter?

+4
source share
5 answers

CustomTraceListener comes from TraceListener, it has a StringDictionary named Attributes.

This will contain all the attributes in the configuration line for your TraceListener and can be displayed by name, for example.

 string logFileName= Attributes["fileName"] 
+6
source

I suspect that perhaps the Enterprise Application Blocks, although (possibly) wonderful, seem unnecessarily complex and, ultimately, more complex than their value for this kind of customization.

+1
source

the problem is typical microsoft .. (add your adjectives here).

1) when you add a custom trace listener, the operator 'raw' app.config is added:

  name="Custom Trace Listener" initializeData="" formatter="Text Formatter" /> 

2) pay attention to "initializeData" - this is that a critical error message causes "InitData".

3) So, thatโ€™s all said that you need a constructor that accepts initialization data - in vb parlance:

  sub new (byval initstuff as string) 

4) OR remove 'initializeData = ""' and create a default constructor:

  sub new() 

I suspect that P&P people live in a bubble. riix.

+1
source

Why is this important, how did I implement it. In my this.buildCurrPath (), I can read from the configuration file, or in this case I just get the โ€œstart barโ€ for the web application. But it works great for me. I haven't put it in any production code yet, but it should be out soon.

 [ConfigurationElementType(typeof(CustomTraceListenerData))] public class CustomListener: CustomTraceListener { #region Fields (3) private int logSize; StreamWriter sw; #endregion Fields #region Constructors (1) public CustomListener ():base() { string startPath = this.buildCurrPath(); sw = new StreamWriter(startPath + "\\Logs\\test.log"); sw.AutoFlush = true; } 
0
source

I had the same problem (except for Enterprise Library v4.1).

The solution I found is to remove the default constructor and only have a constructor with a string parameter for the ie file name

 public AlwaysClosedTextFileTraceListener (string pathParameter) { logFilePath = pathParameter; } 

Then in app.config enter your path in the initializeData parameter

 <add ... initializeData="C:\Logs\myLog.log" /> 

While this is not recognized by the Entriprise library configuration editor and not as neat as it could be, it works as long as there is only one parameter.

If someone finds out how to do it right, send a message and let us know that it should not be so difficult, of course.

0
source

All Articles