4

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.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Hieu N Trinh
  • 59
  • 1
  • 11
  • Found out why the confusing MyTraceLogger.MyTraceLogger , I have it nested in MyTraceLogger class. I pulled it out and have set the config to MyTraceLogger.MyTextTraceListener, MyTraceLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Now I get error Could not create MyTraceLogger.MyTextTraceListener, MyTraceLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null The other post said to overload a constructor. I do not know which one to overload so I try this one: public MyTextTraceListener() : base() { } and it still say could not create. – Hieu N Trinh Dec 18 '14 at 06:59
  • I tried to public MyTextTraceListener(string name) : base(name) { } and debug it, I see the string it passed in it's my log name, but it's seem that this constructor are getting called like 2 or 3 times and after that I get stackoverflow error. – Hieu N Trinh Dec 18 '14 at 21:03
  • I stumble over this article. http://nicholas.piasecki.name/blog/2009/03/on-textwritertracelistener-inheritance-initializedata-aspnet-and-paths/ – Hieu N Trinh Dec 19 '14 at 05:54

2 Answers2

4

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

Community
  • 1
  • 1
sonne
  • 377
  • 5
  • 20
  • I want to control what type or logging i want at certain time, like just error or warning or information logging. – Hieu N Trinh Dec 17 '14 at 21:41
  • Take a closer look at my answer, it does exactly what config does except in a simpler and more controllable way. You will get the same behaviour using declaration instance method. – sonne Dec 17 '14 at 21:42
  • I have not try that way, but what if I want to change the log name, which i probably can use the appsetting add key. I still want to use the config way if possible just to learn. – Hieu N Trinh Dec 17 '14 at 22:01
  • Implement a string parameter that you can change on demand that would generate the log file name. – sonne Dec 17 '14 at 22:05
0

Why do you put the namespace twice? I think you would just need:

<add name="Spending" type="MyTraceLogger.MyTextTraceListener,MyTraceLogger" initializeData="Spending.log" />
Pokechu22
  • 4,984
  • 9
  • 37
  • 62
John
  • 3,627
  • 1
  • 12
  • 13
  • I thought you need the assembly name and namespace name in it. Anyway I already try the one you suggested, same error. – Hieu N Trinh Dec 17 '14 at 21:16
  • You can get the correct string to use with typeof(MyTraceLogger).AssemblyQualifiedName – John Dec 17 '14 at 21:23
  • This is what it came out: MyTraceLogger.MyTraceLogger, MyTraceLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Then I change it in the web.config to: MyTraceLogger.MyTraceLogger.TextWriterTraceListener, MyTraceLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Same error – Hieu N Trinh Dec 17 '14 at 21:31
  • What is your application's name, assembly name? – sonne Dec 17 '14 at 22:12
  • My solution name demo, have 7 projects in it. one of it is the MyTraceLogger that have the MyTextTraceListener. Another project reference to MyTranceLogger and use TraceSource. When I right-click on MyTraceLogger's project for properties, it show assembly is MyTraceLogger and my namespace is also MyTraceLogger. Should I change the namespace? I don't know why both asssembly and namespace are the same, I never notice it. – Hieu N Trinh Dec 18 '14 at 03:18