I saw two answers about this topic but I can't figure out. I have a custom TextWriterTraceListener and I want it to use in my tracesource.
namespace MyTraceLogger
{
public class MyTextTraceListener : TextWriterTraceListener
{
public override void Write(string message)
{
this.Write(string.Format("{0},{1}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
message));
}
public override void WriteLine(string message)
{
this.WriteLine(string.Format("{0},{1}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
message));
}
}
}
<system.diagnostics>
<sources>
<source name="SpendingTrace" switchName="SpendingSourceSwitch" switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<add name="Spending" type="MyTraceLogger.MyTraceLogger.MyTextTraceListener,MyTraceLogger.MyTraceLogger" initializeData="Spending.log" />
<remove name ="Default" />
</listeners>
</source>
</sources>
<switches>
<!-- You can set the level at which tracing is to occur -->
<add name="SpendingSourceSwitch" value="Warning" />
<!-- You can turn tracing off -->
<!--add name="SourceSwitch" value="Off" -->
</switches>
<trace autoflush="true" indentsize="4"></trace>
This is the error I get: Couldn't find type for class MyTraceLogger.MyTraceLogger.MyTextTraceListener,MyTraceLogger.MyTraceLogger.
When I right-click on MyTraceLogger's project for properties, it show assembly is MyTraceLogger and my namespace is also MyTraceLogger.
Why use configuration file and not instantiate it locally inside the code? In such way you will avoid config files being confused, moved or used incorrectly in the future development and maintenance.
Create an instance of your MyTextTraceListener and add it to trace listeners:
MyTextTraceListener myTraceListener = new MyTextTraceListener ("application.log");
Trace.Listeners.Add(myTraceListener);
Refer to this post too: How to define custom TraceListener in app.config