简体   繁体   English

在Log4net中将控制台附加程序添加到Windows窗体

[英]Adding console appender to a windows form in log4net

Is it possible to add a console to a form based C# application ? 是否可以将控制台添加到基于表单的C#应用​​程序中? Currently when I do something like 目前,当我做类似的事情

Console.WriteLine("testing");

It appears in output window of the VS2010. 它出现在VS2010的输出窗口中。 I want to know if its possible to attach a console to my windows form application.So that the output appears in the console. 我想知道是否可以将控制台附加到我的Windows窗体应用程序。以便输出出现在控制台中。

EDIT: Looks like my first question was a bit misleading and it did not exactly specify what I wanted to accomplish. 编辑:看起来我的第一个问题有点误导,它没有确切说明我想要完成的工作。 I just added a console to my application using 我刚刚使用将控制台添加到我的应用程序中

    [DllImport("kernel32")]
    static extern int AllocConsole();

However what I really want is the output of log4net console appender to be displayed in that console which is not happening. 但是,我真正想要的是log4net控制台附加程序的输出,该输出不会在该控制台中显示。 The xml for my appender is 我的appender的xml是

  <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
      <level value="INFO" />
      <foreColor value="White" />
      <backColor value="Red, HighIntensity" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%class %date - %message %newline" />
    </layout>
  </appender>

now when I go like 现在,当我像

 log.info("Some log");

It still does not display it in the newly added console window. 它仍然不会在新添加的控制台窗口中显示它。 Any suggestions on how i could do that ? 关于我该怎么做的任何建议?

Just to throw it out there, be sure that you AllocConsole() before you load your log4net configuration. 只是将其丢到那里, 加载log4net配置之前 ,请确保您已使用AllocConsole() I tried doing something similar to what your question asks, and had the same problem before moving my call to AllocConsole . 在将呼叫移至AllocConsole之前,我尝试过执行与您的问题类似的操作,并且遇到了相同的问题。 Once I moved it, log4net automatically wrote to the console that I allocated. 移动它后,log4net会自动写入我分配的控制台。

Essentially ... (and remember to do all your regular error checking not included here) ... 本质上是...(并且请记住进行所有常规错误检查,此处未包括)...

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SampleApp
{
    class Program
    {
        [DllImport("kernel32.dll", SetLastError=true, CallingConvention=CallingConvention.Winapi)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool AllocConsole();

        [DllImport("kernel32.dll", SetLastError=true, CallingConvention=CallingConvention.Winapi)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool FreeConsole();

        [STAThread]
        private static void Main(string[] args)
        {
            // (1) Make sure we have a console to use.
            Program.AllocConsole();
            try {
                // (2) Tell log4net to configure itself according to our app.config data.
                log4net.Config.XmlConfigurator.Configure();
                // (3) Usual WinForms startup code here.
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new SampleApp.Form1());
            } catch ( Exception ) {
                // WAT!
            }
            // (4) Remember to release the console before we exit.
            Program.FreeConsole();
        }
    }
}

Not 100% sure why it makes a difference as to when the console is allocated, but this did fix the problem for me. 不能100%确定为什么分配控制台的时间有所不同,但这确实为我解决了问题。

只需使您的项目成为控制台应用程序,然后从控制台应用程序创建/显示表单即可,而不是相反。

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

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