Click to See Complete Forum and Search --> : Using the TraceSwitch class to create a LOG-File


Spuuky
July 22nd, 2004, 06:49 AM
Hi,

i have some Problems with the use of the TracListener class. First i will describe my code and at the end I will describe my Problem.

I want to use the TraceSwitch class to write a Log file with variated Path and Directory:

private static TraceSwitch _traceSwitch = new TraceSwitch("myTraceSwitch","TraceLevel für Log-Ausgabe");

...
Trace.WriteLineIf(_traceSwitch.TraceError,"[Error] - Fehlermeldung...");

My App.confi is described in the following part:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<switches>
<add name="myTraceSwitch" value="3" />
</switches>
<trace autoflush="true" indentsize="3">
<listeners>
<add name="MyListener" type="LGT.TraceListenerExt.FileTraceListener,TraceListenerExt"
initializeData="ARI_LOG" />
</listeners>
</trace>
</system.diagnostics>
</configuration>

The TraceSwitch starts a object of my overlaoded TraceListener class. This class is shown in the following:

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Messaging;


namespace MTU.LGT.TraceListenerExt
{
public class FileTraceListener : TextWriterTraceListener
{
public FileTraceListener(Stream stream) : base(stream)
{
}

public FileTraceListener(string fileName) : base(fileName + DateTime.Now.ToString(FileNameSufix))
{
}

public override void WriteLine(string message)
{
base.WriteLine(MsgFormatter.FormatMessage(message));
}

public override void Write(string message)
{
base.Write(MsgFormatter.FormatMessage(message));
}

static public string FileNameSufix
{
set { _filenameSufix = value; }
get { return _filenameSufix; }
}

static protected string _filenameSufix = "_yyyy-MM-dd_HH-mm-ss";
static protected StreamWriter _writer = null;

private MessageFormatter MsgFormatter = new MessageFormatter();
}


public class MessageFormatter
{
public string FormatStringSufix
{
set { _formatStringSufix = value; }
get { return _formatStringSufix; }
}

public string FormatStringPrefix
{
set { _formatStringPrefix = value; }
get { return _formatStringPrefix; }
}

public string FormatMessage(string message)
{
return DateTime.Now.ToString(FormatStringPrefix) + message;
}

protected string _formatStringPrefix = "[yyyy-MM-dd HH:mm:ss] ";
protected string _formatStringSufix = "";
}
}

My Problem ist now:
The TraceSwitch starts the constructor of my TraceListener class and gives them a varaible witch is located in the the APp.conif "initializeData".
Is ther any posiblity to make this initializeData varaible, to make the Name and the Directory of my LOG-File variable, too??

Or do I have to overload a method of the Framework TraceListener calls??


Thanks for any ideas and help!

Spuuky



[Andreas]: Added code tags...