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?