Log4net不会登录到控制台(WinForms应用程序) [英] Log4net won't log to console (WinForms app)

查看:95
本文介绍了Log4net不会登录到控制台(WinForms应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Log4Net库,并且在配置它时遇到问题.我不需要什么特别的东西.我正在将其用于Winforms应用程序,并且需要基本的文件和控制台日志记录.为了使它尽可能简单,我使用App.config进行配置,并使用从Log4Net项目网站获取的默认值: App.config:

I'm just starting using the Log4Net library and having problems configuring it. I don't need anything special. I'm using it for a Winforms application and need basic file and console logging. To keep it as simple as possible, I'm using the App.config for configuration and using the default values taken from Log4Net project website: App.config:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="ProjectFolder" value="D:\Documents\my documents\Themis\Projects"/>
  </appSettings>
  <configSections>
    <section name="log4net"
       type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="ThemisLog.txt" />
      <param name="AppendToFile" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

程序类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using log4net;
using log4net.Config;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Themis
{
    static class Program
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            log4net.Config.XmlConfigurator.Configure();

            log.Debug("Enter application");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new OldFrmMain());
            log.Debug("Exit application");
        }
    }
}

已创建日志文件并将日志创建到其中,但是没有控制台日志记录发生.

The log file is created and logs are created into it, but no console logging happens.

推荐答案

我猜Log4net使用常规的Console.WriteLine(…)方法将消息发送到控制台.由于在默认情况下Console.WriteLine(…)在WinForms应用程序中不执行任何操作,因此它在WinForms应用程序中将不起作用.

I guess Log4net uses the conventional Console.WriteLine(…) method to send messages to console. It will not work in WinForms application because Console.WriteLine(…) does nothing in WinForms application by default.

尝试在应用程序的开头调用Win32 API函数AllocConsole.它应该为您的WinForms应用程序创建一个控制台,并启用Console.WriteLine(…)函数. 在这里,您可以找到显示如何调用AllocConsole的代码示例. 如何在Windows应用

Try to call Win32 API function AllocConsole at the beginning of your application. It should create a console for your WinForms application and enable Console.WriteLine(…) function. Here you can find an example of the code that shows how to call AllocConsole. How to open console window in Windows Apllication

这篇关于Log4net不会登录到控制台(WinForms应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆