2012-10-01 72 views
2

我需要從網站上的代碼輸出一些調試信息。如何從ASP.net網站寫入OutputDebugString?

如何從ASP.net網站撥打電話OutputDebugString並讓用戶看到運行DbgView

注意:網站不支持System.Diagnostics.Trace.TraceWarning(...)

+0

看看這個,讓我知道這樣做是否解決您的問題:http://blog.aggregatedintelligence.com/2010/12/debugview-doesnt-work-with-aspnet-app.html。 –

+0

這是Systems.Diagnostics TraceSource的默認偵聽器。跟蹤將顯示在服務器上的DbgView中。你說「用戶」,你不是指訪問你網站的人,對嗎?爲此,您必須以某種方式編寫自定義偵聽器來寫入HTML響應。 – MatthewMartin

+0

@MatthewMartin哦不,我的意思是在* server *上運行'DbgView'的人(即我)。我知道Windows不支持讓你看到由服務生成的OutputDebugStrings - 因此是一個問題。 –

回答

0

好的,這裏是一個完整的例子。這是一個控制檯,但原則和代碼大致相同。我今天無法在我的機器上測試從OutputDebugString捕獲,因爲我沒有管理員權限。在服務器上,您將寫入TextWriterTraceListener而不是控制檯。如果您無法使用pinvoke寫入和讀取OutputDebugString,可能客戶沒有權限或應用程序沒有必要的權限。

另外!如果Debug.WriteLine沒有顯示出來,那麼網站可能是在RELEASE模式下編譯的,DEBUG沒有定義。 TRACE默認定義爲RELEASE和DEBUG。 TraceSource寫入OutputDebugString,除非你已經清除了默認監聽器,很多人都習慣這樣做,因爲根據我的經驗,OutputDebugString可能會減慢速度,尤其是當你實際上並沒有在查看輸出時。

using System.Diagnostics; 
using System.Runtime.InteropServices; 

namespace TraceToOutputDebugString 
{ 
    class Program 
    { 
     [DllImport("kernel32.dll")] 
     static extern void OutputDebugString(string lpOutputString); 

     static void Main(string[] args) 
     { 
      //Put these lines in your asp.net Page 
      OutputDebugString("Hello from Pinvoke OutputDebugString"); 
      TraceSource trace = new TraceSource("app"); 
      trace.TraceInformation("Hello from TraceSource"); 
      Trace.TraceInformation("Hello from 1.1 Trace.TraceInformation"); 
      Debug.WriteLine("Hello Debug.WriteLine"); 
      System.Console.ReadKey(); 
     } 
    } 
} 

這裏是配置。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.diagnostics> 
     <sources> 
      <source name="app" switchName="app"> 
       <listeners> 
        <add name="Console" type="System.Diagnostics.ConsoleTraceListener"/> 
       </listeners> 
      </source> 
     </sources> 
     <switches> 
      <add name="app" value="Information"/> 
     </switches> 
     <trace> 
      <listeners> 
       <add name="Console" type="System.Diagnostics.ConsoleTraceListener"/> 
      </listeners> 
     </trace> 
    </system.diagnostics> 
</configuration>