Redirect output window to log file

When my application starts, I see that the 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 a timestamp) for some performance measurements. I tried to do this with the next class that 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 that does not work. The Write () method is never called. Any ideas?

+5
source share
3 answers

. , , SDK. DLL 6, LOAD_DLL_DEBUG_EVENT. , , "". " " .

. -, , . Windows , , . , IDE , .

, . , MDbg. .

+2

DebugView File- > SaveoutputAs Visual Studio

+2

, AppDomain.AssemblyLoad. :

using System;

namespace ConsoleApplicationN
{
    class Program
    {
        {
            AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(CurrentDomain_AssemblyLoad);

            // You need to *DO* something here to trigger an assembly load
            // For example, open a database connection
        }

        static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
        {
            // This is just an example of how to use what provided in "args"
            Console.WriteLine(args.LoadedAssembly.FullName);
        }
    }
}

It will not capture everything you need and will not provide the same format as the Visual Studio output window, but it may be an acceptable halfway at home between this and nothing.

0
source

All Articles