CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2002
    Posts
    788

    How to use Trace

    I want to use trace to file.
    Code:
      FileStream Log = new FileStream("Log.txt", FileMode.OpenOrCreate);
    
          Trace.Listeners.Add(new TextWriterTraceListener(Log));
          Trace.WriteLine("My Trace String To Log File");
          Trace.Flush();
          Log.Close();
    The problem with the above codes is trace will always log to the file Log.txt.
    How to call Trace with the file handler itself??, so that i can specify which file to log to when i do a Trace.

    I tried using traceSource like the following, but it doesn';t work as well., the information is not even logged into file!!
    Code:
         {
                    string l_path = String.Format("c:\\mylog_{0}.txt", this.GetHashCode());
                    _log = new FileStream(String.Format(l_path), FileMode.Append);
    
                }
                if (_traceSrc == null)
                {
                    _traceSrc = new TraceSource(this.GetHashCode().ToString());
                    _traceSrc.Listeners.Add(new TextWriterTraceListener(_log));
                    _traceSrc.TraceInformation(String.Format("Hello from {0}", GetHashCode()));

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: How to use Trace

    so you want to be able to log to multiple log files?

  3. #3
    Join Date
    Jul 2002
    Posts
    788

    Re: How to use Trace

    yes..

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: How to use Trace

    Same log in every file, or some information to one file and some to another? Than I'd recommend you to use some kind of logging framework, e.g. Logging Application Block of Microsoft's Enterprise Library.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured