Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect output window to log file

Tags:

c#

.net

When my app starts, I see that following lines are written to the output window:

'MyApp.exe' (Managed (v2.0.50727)): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\PresentationFramework.Luna\3.0.0.0__31bf3856ad364e35\PresentationFramework.Luna.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.exe' (Managed (v2.0.50727)): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\PresentationFramework.Aero\3.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.exe' (Managed (v2.0.50727)): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\PresentationFramework.resources\3.0.0.0_nl_31bf3856ad364e35\PresentationFramework.resources.dll'
'MyApp.exe' (Managed (v2.0.50727)): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Data.SqlServerCe\3.5.1.0__89845dcd8080cc91\System.Data.SqlServerCe.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.exe' (Managed (v2.0.50727)): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Transactions\2.0.0.0__b77a5c561934e089\System.Transactions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.exe' (Managed (v2.0.50727)): Loaded 'C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.exe' (Managed (v2.0.50727)): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Data.resources\2.0.0.0_nl_b77a5c561934e089\System.Data.resources.dll'
'MyApp.exe' (Managed (v2.0.50727)): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.exe' (Managed (v2.0.50727)): Loaded 'Anonymously Hosted DynamicMethods Assembly'

I like to add these lines to my log file (and assign timestamp) for some performance measurements. I've tried to do that with the following class I created.

public static class ConsoleLogger
{
    public class LogWriter : TextWriter
    {
        public LogWriter()
        {
        }

        public override Encoding Encoding
        {
            get { return Encoding.UTF8; }
        }

        public override void Write(string value)
        {
            Logger.Info(value);                
        }
    }

    public static void RedirectConsoleLog()
    {
        Console.SetOut(new LogWriter());
    }
}

But this doesn't work. The Write() method is never called. Any ideas?

like image 369
Robbert Dam Avatar asked Oct 11 '22 06:10

Robbert Dam


1 Answers

These lines are generated by the debugger. The kind of notifications that can be generated by a debugger are documented in this SDK article. The DLL load notification corresponds with event 6, LOAD_DLL_DEBUG_EVENT. The list pretty much corresponds with what you find back in the context menu when you right-click the Output window. "Module Load Messages" for the ones you're interested in.

This has several implications. First off, only a debugger can ever get these notifications, you normally run your program without one after it is deployed. A hard restriction in Windows is that a debugger must be a separate process, a program cannot debug itself. The last nail in your plan is that the IDE doesn't support redirecting what is now sent to the Output window into a file.

You can't make this work unless you write your own debugger. Technically possible, download the MDbg sample. Practically not.

like image 135
Hans Passant Avatar answered Oct 15 '22 11:10

Hans Passant