locked
System.Diagnostics.Trace listener not working RRS feed

  • Question

  • Hi

    I can't get the trace listener to work. I have a Website project in VS2008
    with the following web.config entries:

    <system.diagnostics>
        <trace autoflush="false" indentsize="4">
            <listeners>
                <add name="myListener"
                    type="System.Diagnostics.TextWriterTraceListener"
                    initializeData="C:\Users\Public\Documents\TextWriterOutput.log"
    />
                <remove name="Default" />
          </listeners>
        </trace>
    </system.diagnostics>

    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp"
                    extension=".cs"
                    warningLevel="4"
                    compilerOptions="/d:TRACE"
                    type="Microsoft.CSharp.CSharpCodeProvider, System,
    Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <providerOption name="CompilerVersion" value="v3.5"/>
    <providerOption name="WarnAsError" value="false"/>
    </compiler>
    </compilers>
    </system.codedom>


    This is the Default.aspx page load method:

        protected void Page_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Trace.WriteLine("Test");
        }

    The funny thing is that it did work then just stopped for no apparent
    reason. I've restarted everything I can thing of which might make a
    difference. I'm using Windows 2008 x64, VS2008 with latest patches etc.

    Any ideas?
    Thanks
    Andrew
    Tuesday, January 26, 2010 2:46 PM

Answers

  • Hi Andrew,

    In this situation you can add one event handler for Global Application class's Application_Error event. Then use Server.GetLastError() to get the required information in this situation.

    namespace WebApplication2
    {
        public class Global : System.Web.HttpApplication
        {

            protected void Application_Start(object sender, EventArgs e)
            {

            }

            protected void Session_Start(object sender, EventArgs e)
            {

            }

            protected void Application_BeginRequest(object sender, EventArgs e)
            {

            }

            protected void Application_AuthenticateRequest(object sender, EventArgs e)
            {

            }

            protected void Application_Error(object sender, EventArgs e)
            {
               
            }

            protected void Session_End(object sender, EventArgs e)
            {

            }

            protected void Application_End(object sender, EventArgs e)
            {

            }
        }
    }


    Please have a look at Walkthrough: Integrating ASP.NET Tracing with System.Diagnostics Tracing<!-- -->. The best practice is that we save the trace information in the application root directory. If we don't save the trace information to application root directory, we have to grant Write permission to the trace log for the ASP.NET worker process account.
    http://msdn.microsoft.com/en-us/library/b0ectfxd.aspx

    The initializeData attribute in the previous step has a placeholder value for the ASP.NET application root directory. If you try to a write trace messages to a file other than your application root directory, you might receive a security exception. By default, ASP.NET does not allow writing files to locations on disk that are outside the application root directory, such as C:\. Additionally, you might have to grant Write permission to the trace log for the ASP.NET worker process account.



    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Riquel_Dong Wednesday, February 3, 2010 2:53 AM
    Thursday, January 28, 2010 5:43 AM

All replies

  • Ah, ok I see what the problem is:

    The website has impersonate set to true.

    I was using a windows account which didn't have permission to write to the file path set in the listener:

    initializeData="C:\path_without_permission\SharePoint.log".

    Also no exception is raised by the listener so I had nothing to go on.

    Is there a better way of knowing if listeners are not correctly setup?

    Thanks
    Andrew

    Tuesday, January 26, 2010 3:48 PM
  • Hi Andrew,

    In this situation you can add one event handler for Global Application class's Application_Error event. Then use Server.GetLastError() to get the required information in this situation.

    namespace WebApplication2
    {
        public class Global : System.Web.HttpApplication
        {

            protected void Application_Start(object sender, EventArgs e)
            {

            }

            protected void Session_Start(object sender, EventArgs e)
            {

            }

            protected void Application_BeginRequest(object sender, EventArgs e)
            {

            }

            protected void Application_AuthenticateRequest(object sender, EventArgs e)
            {

            }

            protected void Application_Error(object sender, EventArgs e)
            {
               
            }

            protected void Session_End(object sender, EventArgs e)
            {

            }

            protected void Application_End(object sender, EventArgs e)
            {

            }
        }
    }


    Please have a look at Walkthrough: Integrating ASP.NET Tracing with System.Diagnostics Tracing<!-- -->. The best practice is that we save the trace information in the application root directory. If we don't save the trace information to application root directory, we have to grant Write permission to the trace log for the ASP.NET worker process account.
    http://msdn.microsoft.com/en-us/library/b0ectfxd.aspx

    The initializeData attribute in the previous step has a placeholder value for the ASP.NET application root directory. If you try to a write trace messages to a file other than your application root directory, you might receive a security exception. By default, ASP.NET does not allow writing files to locations on disk that are outside the application root directory, such as C:\. Additionally, you might have to grant Write permission to the trace log for the ASP.NET worker process account.



    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Riquel_Dong Wednesday, February 3, 2010 2:53 AM
    Thursday, January 28, 2010 5:43 AM
  • Hi Andrew,

    Whether my reply can give you help on this issue. If you have any further discussion on this issue, feel free to discuss it here.

    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, February 2, 2010 9:09 AM