log4net的将无法登录到控制台 [英] Log4net won't log to console

查看:83
本文介绍了log4net的将无法登录到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用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>

Program类:

Program class:

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(...)方法将消息发送到控制台。它不会在WinForms应用程序工作,因为Console.WriteLine(...)什么也不做的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(...)函数。 在这里,你可以找到的code,显示了如何调用AllocConsole一个例子。 <一href="http://social.msdn.microsoft.com/forums/en-US/csharplanguage/thread/b7a14400-6d72-4fbf-9927-0966f69ef4a2/"相对=nofollow>如何在Windows Apllication 打开控制台窗口

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的将无法登录到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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