2

i need to output some debugging information from code on a web-site.

How can i call OutputDebugString from an ASP.net web-site, and have it appear to users running DbgView?

Note: Web-sites do not support System.Diagnostics.Trace.TraceWarning(...).

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Take a look at this and let me know if this solves your issue: http://blog.aggregatedintelligence.com/2010/12/debugview-doesnt-work-with-aspnet-app.html. – Lukasz M Oct 01 '12 at 18:17
  • That is the default listener for Systems.Diagnostics TraceSource. The trace will appear in DbgView on the server. You said "Users", you don't mean the people visiting your website, right? For that you'd have to somehow write a custom listener to write to the HTML response. – MatthewMartin Oct 17 '12 at 20:52
  • @MatthewMartin Oh no, i meant someone (i.e. me) on the *server* running `DbgView`. i know that Windows doesn't support letting you see OutputDebugStrings that were generated by a service - hence the question. – Ian Boyd Oct 18 '12 at 02:11

1 Answers1

-1

Okay, here is a complete example. It's a console but the principles and the code are much the same. I couldn't test capturing from OutputDebugString on my machine today because I don't have admin rights. On a server, you'd write to TextWriterTraceListener instead of a console. If you can't write and read from OutputDebugString using pinvoke, may the customer doesn't have the rights or the app doesn't have the necessary rights.

Also! If the Debug.WriteLine isn't showing up, maybe the website is compiled in RELEASE mode and DEBUG isn't define. TRACE by default is defined for RELEASE And DEBUG. TraceSource writes to OutputDebugString unless you've cleared the default listener, which a lot of people do as a matter of habit since OutputDebugString in my experience can slow things down esp if you aren't actually looking at the output at the moment.

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();
        }
    }
}

And here is the config.

<?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>
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164