【问题标题】:C# TraceListner not working when published to IISC# TraceListner 在发布到 IIS 时不起作用
【发布时间】:2015-12-05 22:18:27
【问题描述】:

以下代码在 Visual Studios 中可以很好地返回跟踪输出,但是当我尝试在 IIS 上的 aspx 页面中运行相同的代码时,它会停止工作。这可能吗?如果可以,我错过了什么?

            System.Diagnostics.Trace.AutoFlush = true;
            StringBuilder traceBuilder = new StringBuilder();
            StringWriter stringWriter = new StringWriter(traceBuilder);
            using (System.Diagnostics.TextWriterTraceListener textWriterTraceListener = new TextWriterTraceListener(stringWriter))
            {
                System.Diagnostics.Trace.Listeners.Add(textWriterTraceListener);
                System.Diagnostics.Trace.WriteLine("HELLO WORLD!");
                System.Diagnostics.Trace.Flush();
                textWriterTraceListener.Flush();
            }

            stringWriter.Flush();
            string traceOutput = traceBuilder.ToString();
            HttpContext.Current.Response.Write("traceOutput - " + traceOutput);

【问题讨论】:

  • traceOutput 输出如何?
  • 嗯,这很奇怪。我会设置断点并单步执行代码以查看发生了什么。
  • 当我调试它跳过 Trace.WriteLine 代码行。所以这就解释了为什么我没有得到输出,但为什么它跳过了那行代码?

标签: c# asp.net iis tracelistener


【解决方案1】:

必须在 web.config 文件中添加以下内容:

<system.codedom>
 <compilers>
  <compiler language="c#;cs;csharp" 
            extension=".cs" 
            compilerOptions="/d:TRACE"
            type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="1" />
 </compilers>
</system.codedom>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2018-11-24
    相关资源
    最近更新 更多