开发者

How can i change the output format of TextWriterTraceListener?

开发者 https://www.devze.com 2022-12-19 09:21 出处:网络
the default implementation shows the output as \': : \', now i dont want this format, i want to cut off the sourcename and eventid because it\' s totally useless for me.

the default implementation shows the output as ': : ', now i dont want this format, i want to cut off the sourcename and eventid because it' s totally useless for me. is there any easy way i can accomplis开发者_开发问答h that?

my current workaround is to derive from TextWriterTraceListener and override the TrackEvent method. use reflector to copy the default implementation and just remove the writerHeader function call.


you can create derived class from TextWriterTraceListener and override TraceEvent to change output format. It would be better to override all TraceEvent methods. e.g.

class TextWriterTraceListenerCustomOutput:TextWriterTraceListener
{
    public TextWriterTraceListenerCustomOutput(string fileName):base(fileName)
    {
    }

    public override void Write(string message)
    {
        base.Write(String.Format("[{0}]:{1}",DateTime.Now,message));
    }

    public override void WriteLine(string message)
    {
        base.WriteLine(String.Format("[{0}]:{1}", DateTime.Now, message));
    }

    public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
    {
        //base.TraceEvent(eventCache, source, eventType, id, message);
        if(String.IsNullOrEmpty(message)) return;
        WriteLine(String.Format("{0}:{1}:{2}:{3}",source, eventType,Enum.GetName(typeof(LogId),id),message.Replace("\r",string.Empty).Replace("\n",string.Empty)));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号