Log4net won't log to console (WinForms app)
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 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.
What you want is not ConsoleAppender
but TraceAppender
.
The name can be confusing, but TraceAppender writes in Visual Studio's "Output" window.
Sample:
<log4net>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="TraceAppender"/>
</root>
</log4net>
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.
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
Try using the OutputDebugStringAppender in conjunction with Sysinternals DebugView it is a good alternative to opening a console window in a WinForms application
You should try setting your type of app to Console Application, instead of Windows Application. You then get both worlds:
1) console
2) a winforms app
Project -> properties -> Application -> outputtype = Windows Application
Switch to:
Project -> properties -> Application -> outputtype = Console Application
In my case, I get the appender logs from log4net to the console, using the configured (colored)consoleappender.
I would recommend modifying your config file a bit. First, I'm not sure you should be using the header and footer entries for the ConsoleAppender. Second, I don't think you need the param= text either. I'm not saying either of these is necessarily wrong, but let's see if we can make the ConsoleAppender as simple as possible. Try the following line as the only line inside the LayoutPattern tags:
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newline"/>
I know that line works because I have used it myself in the past. Let me know if running just that works.
Here is an article that explains in more detail each section and what it does: http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
is case sensitive !!
log4net: config file [..\Log4Net.config] not found. Configuration unchanged.
精彩评论