none
Set output file for Trace.Write/Trace.WriteLine functions RRS feed

  • Question

  • I want to use Trace.Write/Trace.WriteLine, how do I set a specific file name for my tracing?
    • Moved by Lisa Zhu Wednesday, December 12, 2012 8:11 AM (From:Visual C# General)
    Monday, December 10, 2012 7:29 PM

Answers

All replies

  • Use the TextWriterTraceListener  class with the Trace


    Roar Jørstad aka sveroa
    Senior Consultant, EVRY as
    Blog: Notebook, trick & tips
    Please mark as answer or helpful if my post is useful

    • Proposed as answer by Mike FengModerator Wednesday, December 12, 2012 10:19 AM
    • Marked as answer by jamesiw Thursday, December 13, 2012 4:55 PM
    Monday, December 10, 2012 7:40 PM
  • Hi jamesiw,

    From your description, I ‘d like to move this post to  the most related forum.

    There are more  experts in this aspect, so you will get  better support and  may have more luck getting answers.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 12, 2012 8:10 AM
  • Hi Jamesiw,

    Welcome to the MSDN Forum.

    Additional to Sveroa's solution:

    Please check the Listeners property of Trace class: http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.listeners.aspx 

    Before you call Trace.Write, you need to add at least one listener to the listeners collection. Based on your specific scenario, you can initialize the TextWriterTraceListener by this constructor: http://msdn.microsoft.com/en-us/library/dt303dc6.aspx the parameter is represented a file name.

    After this, the log will be written to your specific file.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 12, 2012 10:24 AM
    Moderator
  • I'm sure you've provided a correct answer, but it just isn't working in my code.  I am implementing this on a WCF service in the Global.asax.cs code, and nothing is being recorded, I do a search on the computer for the filename, it's not found, and nowhere in my WCF project directories to I see the file.  The debugger is going through the lines where e.Authenticated == true, in other words, it should write "blah" to the file, but no file appears.

    void AuthenticationService_Authenticating(object sender, System.Web.ApplicationServices.AuthenticatingEventArgs e)
    {
    
    
        e.Authenticated = new UserValidator().IsUserValid(e.UserName, e.Password) && WindowsIdentity.GetCurrent().IsAuthenticated;
        e.AuthenticationIsComplete = true;
        TextWriterTraceListener twTraceListener;
        twTraceListener = new TextWriterTraceListener("sampleservice_messages.txt");
        Trace.Listeners.Add(twTraceListener);
        if (e.Authenticated)
        {
            Trace.WriteLine("blah");
            Trace.Flush();
            twTraceListener.Flush();
            twTraceListener.Close();
            Trace.Close();
            currentUserName = e.UserName;
        }
        else
        {
            Trace.WriteLine(string.Format("failed to login, username: {0}", e.UserName));
            Trace.Flush();
            twTraceListener.Flush();
            twTraceListener.Close();
            Trace.Close();
        }
    }






    • Edited by jamesiw Wednesday, December 12, 2012 3:31 PM
    Wednesday, December 12, 2012 3:24 PM
  • Hi Jamesiw,

    Please surround a tr-catch block to check whether an exception occurs when you create a log file by 

    TextWriterTraceListener

    And you can also try a absolute path file.

    I hope the troubleshooting helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 13, 2012 4:11 AM
    Moderator
  • I noticed the file wasn't created, so I had to create the file first.  But since it was running on WCF I had to go into directory properties and set the permissions to access the file (For File.Create(pathAndFileName)) using the default application pool as the identity.  But basically, the answer that sveroa gave is correct, the file just had to be created. I was able to google around to get the permissions things worked out, I got the error access to path denied and googled it, and figured out everything that wasn't posted here.  Thanks to everyone for helping or attempting to help.
    Thursday, December 13, 2012 5:02 PM