简体   繁体   English

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

[英]Redirect output window to log file

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. 永远不会调用Write()方法。 Any ideas? 有任何想法吗?

您可以在Visual Studio中使用DebugView或File-> SaveoutputAs

These lines are generated by the debugger. 这些行由调试器生成。 The kind of notifications that can be generated by a debugger are documented in this SDK article . 此SDK文章中介绍了调试程序可以生成的通知类型。 The DLL load notification corresponds with event 6, LOAD_DLL_DEBUG_EVENT. DLL加载通知对应于事件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. Windows中的一个严格限制是调试器必须是一个单独的进程,程序无法自行调试。 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. 计划中的最后一点是IDE不支持将现在发送到“输出”窗口的内容重定向到文件中。

You can't make this work unless you write your own debugger. 除非编写自己的调试器,否则无法完成此工作。 Technically possible, download the MDbg sample . 从技术上讲,可以下载MDbg示例 Practically not. 实际上没有。

The closest you can get to this is to handle the AppDomain.AssemblyLoad event. 您最接近的是处理AppDomain.AssemblyLoad事件。 The simplest example of this is: 最简单的例子是:

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

This won't capture everything you're after, nor will it provide exactly the same format as the Visual Studio Output window but it might be an acceptable "half-way" house between that and nothing. 这不会捕获您所追求的所有内容,也不会提供与Visual Studio输出窗口完全相同的格式,但它可能是一个可接受的“中途”房子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM