将输出窗口重定向到日志文件

| 当我的应用程序启动时,我看到以下行被写入输出窗口:
\'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\'
我想将这些行添加到我的日志文件中(并分配时间戳)以进行一些性能评估。我已经尝试使用我创建的以下类来做到这一点。
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());
    }
}
但这是行不通的。永远不会调用Write()方法。有任何想法吗?
已邀请:
这些行由调试器生成。此SDK文章中介绍了调试器可以生成的通知类型。 DLL加载通知与事件6 LOAD_DLL_DEBUG_EVENT相对应。右键单击“输出”窗口时,该列表几乎与您在上下文菜单中找到的内容相对应。 \“ Module Load Messages \”为您感兴趣的内容。 这有几个含义。首先,只有调试器才能获得这些通知,通常在部署程序后不运行程序就运行程序。 Windows中的一个严格限制是调试器必须是一个单独的进程,程序不能自行调试。您计划中的最后一个问题是IDE不支持将现在发送到“输出”窗口的内容重定向到文件中。 除非您编写自己的调试器,否则您将无法进行此项工作。从技术上讲,请下载MDbg示例。实际上不是。
您可以在Visual Studio中使用DebugView或File-> SaveoutputAs
您可以最接近地处理
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\'s provided in \"args\"
            Console.WriteLine(args.LoadedAssembly.FullName);
        }
    }
}
这不会捕获您所需要的所有内容,也不会提供与Visual Studio输出窗口完全相同的格式,但是这可能是一个可以接受的“半途而废”的东西。

要回复问题请先登录注册