You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Michael SALOMON <mi...@gmail.com> on 2009/06/26 15:21:31 UTC
Problem using a ConsoleAppender in a Windows Form application
Hi !
I'm using log4net 1.2.10.0 and .NET 2.0
I wrote a Windows Form application and I want to be able to log debug
messages in a ColoredConsoleAppender (or whatever type of Console appender).
If I compile my application as a Console Application, this works fine... If
I compile it as a Windows Forms application, then I understand that no
console is attached to my application, fair enough..
In this case I programmaticaly attach a brand new console by using this
piece of code :
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeConsole();
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);
public static void SetConsole()
{
foreach (log4net.Appender.IAppender appender in
log4net.LogManager.GetRepository().GetAppenders())
{
System.Type type = appender.GetType();
if ((type ==
typeof(log4net.Appender.ColoredConsoleAppender))
|| (type == typeof(log4net.Appender.ConsoleAppender)))
{
if (AllocConsole())
AttachConsole(-1);
return;
}
}
So at startup, if any console appender is found, I attach a console...
At this stage, I can do things like : Console.WriteLine() , and it works
very fine, I can see the messages in my new console...
But for some reason, the Console Appenders do not forward their messages to
this console...
Do I miss something or do something wrong ?
I tried to create a custom appender that inherits ConsoleAppender and do
something like:
protected override void Append (LoggingEvent loggingEvent)
{
Console.WriteLine (RenderLoggingEvent(loggingEvent);
}
This works fine with my attached console, but I loose all the nice colored
features... And fact is I do want them :)
Thanks a lot for your help
Michael
Re: Problem using a ConsoleAppender in a Windows Form application
Posted by Michael SALOMON <mi...@gmail.com>.
Hi !!
I truely apologize for this late answer!
This works great for me !! Thanks so much for your help, log4net definitely
rocks !
Regards
michael
On Sat, Jun 27, 2009 at 2:06 AM, Ron Grabowski <ro...@yahoo.com>wrote:
> This works for me:
>
> public partial class Form1 : Form
> {
> [DllImport("kernel32.dll", SetLastError = true)]
> [return: MarshalAs(UnmanagedType.Bool)]
> static extern bool AllocConsole();
>
> [DllImport("kernel32.dll", SetLastError = true)]
> private static extern bool AttachConsole(int dwProcessId);
>
> public Form1()
> {
> InitializeComponent();
>
> if (AllocConsole())
> AttachConsole(-1);
>
> var appender = new ColoredConsoleAppender();
> var mapping = new ColoredConsoleAppender.LevelColors
> {
> Level = Level.Debug,
> ForeColor = ColoredConsoleAppender.Colors.Red,
> BackColor = ColoredConsoleAppender.Colors.Blue
> };
> appender.AddMapping(mapping);
> appender.ActivateOptions();
> appender.Layout = new SimpleLayout();
> BasicConfigurator.Configure(appender);
>
> ILog log = LogManager.GetLogger(typeof(Form1));
> log.Debug("Hello World");
> }
> }
>
> ------------------------------
> *From:* Michael SALOMON <mi...@gmail.com>
> *To:* Log4NET User <lo...@logging.apache.org>
> *Sent:* Friday, June 26, 2009 1:18:49 PM
> *Subject:* Re: Problem using a ConsoleAppender in a Windows Form
> application
>
> Well, I tried this and it still does not work:
>
>
> if (AllocConsole())
> AttachConsole(-1);
>
> // this file exists....
> XmlConfigurator.ConfigureAndWatch(new
> System.IO.FileInfo("./Log4Net.config"));
>
> LogManager.GetLogger("DebugLogger").Debug("message");
>
> My config file:
> <?xml version="1.0" encoding="utf-8" ?>
> <log4net>
> <appender name="DebugConsoleAppender"
> type="log4net.Appender.ColoredConsoleAppender">
> <threshold value="ALL"/>
> <mapping>
> <level value="FATAL" />
> <foreColor value="White" />
> <backColor value="Red, HighIntensity" />
> </mapping>
> <mapping>
> <level value="ERROR" />
> <backColor value="Red, HighIntensity" />
> </mapping>
> <mapping>
> <level value="WARN" />
> <backColor value="Yellow, HighIntensity" />
> </mapping>
> <mapping>
> <level value="DEBUG" />
> <backColor value="Green" />
> </mapping>
> <mapping>
> <level value="INFO" />
> <foreColor value="White" />
> </mapping>
> <layout type="log4net.Layout.PatternLayout">
> <conversionPattern value="%ndc %date %-4timestamp %-5level - %message
> %newline%exception " />
> </layout>
> </appender>
>
> <root>
> <level value="ALL" />
> </root>
>
> <logger name="DebugLogger">
> <level value="ALL"/>
> <appender-ref ref="DebugConsoleAppender"/>
> </logger>
>
> </log4net>
>
>
> Thanks for your help !
>
> Michael
> 2009/6/26 Michael SALOMON <mi...@gmail.com>
>
>> thx for your answer ! i'll try that but then I need to change my approach.
>>
>> Originally I wanted to attach a console depending on log4net config, so
>> now i will try to detach the console depending on the config ;)
>>
>> I don't mind doing that
>>
>> I'll let you know if it works.
>>
>> 2009/6/26 Radovan Raszka <ra...@hasam.cz>
>>
>> Hello,
>>> try to allocate your console _before_ log4net is configured.
>>> Radovan
>>> ------------------------------
>>> *Od:* Michael SALOMON [mailto:michaelsalomon78@gmail.com]
>>> *Odesláno:* 26. června 2009 15:22
>>> *Komu:* log4net-user@logging.apache.org
>>> *Předmět:* Problem using a ConsoleAppender in a Windows Form application
>>>
>>> Hi !
>>>
>>> I'm using log4net 1.2.10.0 and .NET 2.0
>>>
>>> I wrote a Windows Form application and I want to be able to log debug
>>> messages in a ColoredConsoleAppender (or whatever type of Console appender).
>>>
>>> If I compile my application as a Console Application, this works fine...
>>> If I compile it as a Windows Forms application, then I understand that no
>>> console is attached to my application, fair enough..
>>>
>>> In this case I programmaticaly attach a brand new console by using this
>>> piece of code :
>>>
>>> [DllImport("kernel32.dll", SetLastError = true)]
>>> [return: MarshalAs(UnmanagedType.Bool)]
>>> static extern bool AllocConsole();
>>>
>>> [DllImport("kernel32.dll", SetLastError = true)]
>>> [return: MarshalAs(UnmanagedType.Bool)]
>>> static extern bool FreeConsole();
>>>
>>> [DllImport("kernel32.dll", SetLastError = true)]
>>> private static extern bool AttachConsole(int dwProcessId);
>>>
>>> public static void SetConsole()
>>> {
>>> foreach (log4net.Appender.IAppender appender in
>>> log4net.LogManager.GetRepository().GetAppenders())
>>> {
>>> System.Type type = appender.GetType();
>>>
>>> if ((type ==
>>> typeof(log4net.Appender.ColoredConsoleAppender))
>>> || (type ==
>>> typeof(log4net.Appender.ConsoleAppender)))
>>> {
>>> if (AllocConsole())
>>> AttachConsole(-1);
>>>
>>> return;
>>> }
>>> }
>>>
>>> So at startup, if any console appender is found, I attach a console...
>>>
>>> At this stage, I can do things like : Console.WriteLine() , and it works
>>> very fine, I can see the messages in my new console...
>>>
>>> But for some reason, the Console Appenders do not forward their messages
>>> to this console...
>>>
>>> Do I miss something or do something wrong ?
>>>
>>> I tried to create a custom appender that inherits ConsoleAppender and do
>>> something like:
>>>
>>> protected override void Append (LoggingEvent loggingEvent)
>>> {
>>> Console.WriteLine (RenderLoggingEvent(loggingEvent);
>>> }
>>>
>>> This works fine with my attached console, but I loose all the nice
>>> colored features... And fact is I do want them :)
>>>
>>> Thanks a lot for your help
>>>
>>> Michael
>>>
>>
>>
>
Re: Problem using a ConsoleAppender in a Windows Form application
Posted by Ron Grabowski <ro...@yahoo.com>.
This works for me:
public partial class Form1 : Form
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);
public Form1()
{
InitializeComponent();
if (AllocConsole())
AttachConsole(-1);
var appender = new ColoredConsoleAppender();
var mapping = new ColoredConsoleAppender.LevelColors
{
Level = Level.Debug,
ForeColor = ColoredConsoleAppender.Colors.Red,
BackColor = ColoredConsoleAppender.Colors.Blue
};
appender.AddMapping(mapping);
appender.ActivateOptions();
appender.Layout = new SimpleLayout();
BasicConfigurator.Configure(appender);
ILog log = LogManager.GetLogger(typeof(Form1));
log.Debug("Hello World");
}
}
________________________________
From: Michael SALOMON <mi...@gmail.com>
To: Log4NET User <lo...@logging.apache.org>
Sent: Friday, June 26, 2009 1:18:49 PM
Subject: Re: Problem using a ConsoleAppender in a Windows Form application
Well, I tried this and it still does not work:
if (AllocConsole())
AttachConsole(-1);
// this file exists....
XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("./Log4Net.config"));
LogManager.GetLogger("DebugLogger").Debug("message");
My config file:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="DebugConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<threshold value="ALL"/>
<mapping>
<level value="FATAL" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="ERROR" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<backColor value="Yellow, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="White" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%ndc %date %-4timestamp %-5level - %message %newline%exception " />
</layout>
</appender>
<root>
<level value="ALL" />
</root>
<logger name="DebugLogger">
<level value="ALL"/>
<appender-ref ref="DebugConsoleAppender"/>
</logger>
</log4net>
Thanks for your help !
Michael
2009/6/26 Michael SALOMON <mi...@gmail.com>
thx for your answer ! i'll try that but then I need to change my approach.
Originally I wanted to attach a console depending on log4net config, so now i will try to detach the console depending on the config ;)
I don't mind doing that
I'll let you know if it works.
2009/6/26 Radovan Raszka <ra...@hasam.cz>
Hello,
try to allocate your console _before_ log4net is
configured.
Radovan
________________________________
Od: Michael SALOMON
[mailto:michaelsalomon78@gmail.com]
Odesláno: 26. června 2009
15:22
Komu: log4net-user@logging.apache.org
Předmět: Problem
using a ConsoleAppender in a Windows Form application
Hi !
I'm using log4net 1.2.10.0 and .NET 2.0
I wrote a
Windows Form application and I want to be able to log debug messages in a
ColoredConsoleAppender (or whatever type of Console appender).
If I
compile my application as a Console Application, this works fine... If I compile
it as a Windows Forms application, then I understand that no console is attached
to my application, fair enough..
In this case I programmaticaly attach a
brand new console by using this piece of code
:
[DllImport("kernel32.dll",
SetLastError = true)]
[return:
MarshalAs(UnmanagedType.Bool)]
static extern bool
AllocConsole();
[DllImport("kernel32.dll", SetLastError =
true)]
[return:
MarshalAs(UnmanagedType.Bool)]
static extern bool
FreeConsole();
[DllImport("kernel32.dll", SetLastError =
true)]
private static extern bool
AttachConsole(int
dwProcessId);
public static
void SetConsole()
{
foreach
(log4net.Appender.IAppender appender in
log4net.LogManager.GetRepository().GetAppenders())
{
System.Type type =
appender.GetType();
if ((type ==
typeof(log4net.Appender.ColoredConsoleAppender))
|| (type ==
typeof(log4net.Appender.ConsoleAppender)))
{
if
(AllocConsole())
AttachConsole(-1);
return;
}
}
So at startup, if any console appender is found, I attach a
console...
At this stage, I can do things like : Console.WriteLine() ,
and it works very fine, I can see the messages in my new console...
But
for some reason, the Console Appenders do not forward their messages to this
console...
Do I miss something or do something wrong ?
I tried to
create a custom appender that inherits ConsoleAppender and do something
like:
protected override void Append (LoggingEvent
loggingEvent)
{
Console.WriteLine
(RenderLoggingEvent(loggingEvent);
}
This works fine with my attached
console, but I loose all the nice colored features... And fact is I do want them
:)
Thanks a lot for your help
Michael
Re: Problem using a ConsoleAppender in a Windows Form application
Posted by Michael SALOMON <mi...@gmail.com>.
Well, I tried this and it still does not work:
if (AllocConsole())
AttachConsole(-1);
// this file exists....
XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo("./Log4Net.config"));
LogManager.GetLogger("DebugLogger").Debug("message");
My config file:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="DebugConsoleAppender"
type="log4net.Appender.ColoredConsoleAppender">
<threshold value="ALL"/>
<mapping>
<level value="FATAL" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="ERROR" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<backColor value="Yellow, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="White" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%ndc %date %-4timestamp %-5level - %message
%newline%exception " />
</layout>
</appender>
<root>
<level value="ALL" />
</root>
<logger name="DebugLogger">
<level value="ALL"/>
<appender-ref ref="DebugConsoleAppender"/>
</logger>
</log4net>
Thanks for your help !
Michael
2009/6/26 Michael SALOMON <mi...@gmail.com>
> thx for your answer ! i'll try that but then I need to change my approach.
>
> Originally I wanted to attach a console depending on log4net config, so now
> i will try to detach the console depending on the config ;)
>
> I don't mind doing that
>
> I'll let you know if it works.
>
> 2009/6/26 Radovan Raszka <ra...@hasam.cz>
>
> Hello,
>> try to allocate your console _before_ log4net is configured.
>> Radovan
>> ------------------------------
>> *Od:* Michael SALOMON [mailto:michaelsalomon78@gmail.com]
>> *Odesláno:* 26. června 2009 15:22
>> *Komu:* log4net-user@logging.apache.org
>> *Předmět:* Problem using a ConsoleAppender in a Windows Form application
>>
>> Hi !
>>
>> I'm using log4net 1.2.10.0 and .NET 2.0
>>
>> I wrote a Windows Form application and I want to be able to log debug
>> messages in a ColoredConsoleAppender (or whatever type of Console appender).
>>
>> If I compile my application as a Console Application, this works fine...
>> If I compile it as a Windows Forms application, then I understand that no
>> console is attached to my application, fair enough..
>>
>> In this case I programmaticaly attach a brand new console by using this
>> piece of code :
>>
>> [DllImport("kernel32.dll", SetLastError = true)]
>> [return: MarshalAs(UnmanagedType.Bool)]
>> static extern bool AllocConsole();
>>
>> [DllImport("kernel32.dll", SetLastError = true)]
>> [return: MarshalAs(UnmanagedType.Bool)]
>> static extern bool FreeConsole();
>>
>> [DllImport("kernel32.dll", SetLastError = true)]
>> private static extern bool AttachConsole(int dwProcessId);
>>
>> public static void SetConsole()
>> {
>> foreach (log4net.Appender.IAppender appender in
>> log4net.LogManager.GetRepository().GetAppenders())
>> {
>> System.Type type = appender.GetType();
>>
>> if ((type ==
>> typeof(log4net.Appender.ColoredConsoleAppender))
>> || (type == typeof(log4net.Appender.ConsoleAppender)))
>> {
>> if (AllocConsole())
>> AttachConsole(-1);
>>
>> return;
>> }
>> }
>>
>> So at startup, if any console appender is found, I attach a console...
>>
>> At this stage, I can do things like : Console.WriteLine() , and it works
>> very fine, I can see the messages in my new console...
>>
>> But for some reason, the Console Appenders do not forward their messages
>> to this console...
>>
>> Do I miss something or do something wrong ?
>>
>> I tried to create a custom appender that inherits ConsoleAppender and do
>> something like:
>>
>> protected override void Append (LoggingEvent loggingEvent)
>> {
>> Console.WriteLine (RenderLoggingEvent(loggingEvent);
>> }
>>
>> This works fine with my attached console, but I loose all the nice colored
>> features... And fact is I do want them :)
>>
>> Thanks a lot for your help
>>
>> Michael
>>
>
>
Re: Problem using a ConsoleAppender in a Windows Form application
Posted by Michael SALOMON <mi...@gmail.com>.
thx for your answer ! i'll try that but then I need to change my approach.
Originally I wanted to attach a console depending on log4net config, so now
i will try to detach the console depending on the config ;)
I don't mind doing that
I'll let you know if it works.
2009/6/26 Radovan Raszka <ra...@hasam.cz>
> Hello,
> try to allocate your console _before_ log4net is configured.
> Radovan
> ------------------------------
> *Od:* Michael SALOMON [mailto:michaelsalomon78@gmail.com]
> *Odesláno:* 26. června 2009 15:22
> *Komu:* log4net-user@logging.apache.org
> *Předmět:* Problem using a ConsoleAppender in a Windows Form application
>
> Hi !
>
> I'm using log4net 1.2.10.0 and .NET 2.0
>
> I wrote a Windows Form application and I want to be able to log debug
> messages in a ColoredConsoleAppender (or whatever type of Console appender).
>
> If I compile my application as a Console Application, this works fine... If
> I compile it as a Windows Forms application, then I understand that no
> console is attached to my application, fair enough..
>
> In this case I programmaticaly attach a brand new console by using this
> piece of code :
>
> [DllImport("kernel32.dll", SetLastError = true)]
> [return: MarshalAs(UnmanagedType.Bool)]
> static extern bool AllocConsole();
>
> [DllImport("kernel32.dll", SetLastError = true)]
> [return: MarshalAs(UnmanagedType.Bool)]
> static extern bool FreeConsole();
>
> [DllImport("kernel32.dll", SetLastError = true)]
> private static extern bool AttachConsole(int dwProcessId);
>
> public static void SetConsole()
> {
> foreach (log4net.Appender.IAppender appender in
> log4net.LogManager.GetRepository().GetAppenders())
> {
> System.Type type = appender.GetType();
>
> if ((type ==
> typeof(log4net.Appender.ColoredConsoleAppender))
> || (type == typeof(log4net.Appender.ConsoleAppender)))
> {
> if (AllocConsole())
> AttachConsole(-1);
>
> return;
> }
> }
>
> So at startup, if any console appender is found, I attach a console...
>
> At this stage, I can do things like : Console.WriteLine() , and it works
> very fine, I can see the messages in my new console...
>
> But for some reason, the Console Appenders do not forward their messages to
> this console...
>
> Do I miss something or do something wrong ?
>
> I tried to create a custom appender that inherits ConsoleAppender and do
> something like:
>
> protected override void Append (LoggingEvent loggingEvent)
> {
> Console.WriteLine (RenderLoggingEvent(loggingEvent);
> }
>
> This works fine with my attached console, but I loose all the nice colored
> features... And fact is I do want them :)
>
> Thanks a lot for your help
>
> Michael
>
RE: Problem using a ConsoleAppender in a Windows Form application
Posted by Radovan Raszka <ra...@hasam.cz>.
Hello,
try to allocate your console _before_ log4net is configured.
Radovan
________________________________
Od: Michael SALOMON [mailto:michaelsalomon78@gmail.com]
Odesláno: 26. června 2009 15:22
Komu: log4net-user@logging.apache.org
Předmět: Problem using a ConsoleAppender in a Windows Form application
Hi !
I'm using log4net 1.2.10.0 and .NET 2.0
I wrote a Windows Form application and I want to be able to log debug messages in a ColoredConsoleAppender (or whatever type of Console appender).
If I compile my application as a Console Application, this works fine... If I compile it as a Windows Forms application, then I understand that no console is attached to my application, fair enough..
In this case I programmaticaly attach a brand new console by using this piece of code :
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeConsole();
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);
public static void SetConsole()
{
foreach (log4net.Appender.IAppender appender in log4net.LogManager.GetRepository().GetAppenders())
{
System.Type type = appender.GetType();
if ((type == typeof(log4net.Appender.ColoredConsoleAppender))
|| (type == typeof(log4net.Appender.ConsoleAppender)))
{
if (AllocConsole())
AttachConsole(-1);
return;
}
}
So at startup, if any console appender is found, I attach a console...
At this stage, I can do things like : Console.WriteLine() , and it works very fine, I can see the messages in my new console...
But for some reason, the Console Appenders do not forward their messages to this console...
Do I miss something or do something wrong ?
I tried to create a custom appender that inherits ConsoleAppender and do something like:
protected override void Append (LoggingEvent loggingEvent)
{
Console.WriteLine (RenderLoggingEvent(loggingEvent);
}
This works fine with my attached console, but I loose all the nice colored features... And fact is I do want them :)
Thanks a lot for your help
Michael
Re: Problem using a ConsoleAppender in a Windows Form application
Posted by Ron Grabowski <ro...@yahoo.com>.
ColoredConsoleAppender calls Console.OpenStandardOutput() in ActivateOptions to store a Stream that represents the console. When you create a console in a WinForm app you need to call ActivateOptions again so the internal Stream can be re-initialized:
public partial class Form1 : Form
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);
public Form1()
{
InitializeComponent();
var appender = new ColoredConsoleAppender();
var mapping = new ColoredConsoleAppender.LevelColors
{
Level = Level.Debug,
ForeColor = ColoredConsoleAppender.Colors.Red,
BackColor = ColoredConsoleAppender.Colors.Blue
};
appender.AddMapping(mapping);
appender.ActivateOptions();
appender.Layout = new SimpleLayout();
BasicConfigurator.Configure(appender);
SetConsole();
ILog log = LogManager.GetLogger(typeof(Form1));
log.Debug("Hello World");
}
public static void SetConsole()
{
foreach (IAppender appender in LogManager.GetRepository().GetAppenders())
{
var coloredConsole = appender as ColoredConsoleAppender;
if (coloredConsole != null)
{
if (AllocConsole())
{
AttachConsole(-1);
coloredConsole.ActivateOptions();
}
return;
}
}
}
}
________________________________
From: Michael SALOMON <mi...@gmail.com>
To: log4net-user@logging.apache.org
Sent: Friday, June 26, 2009 9:21:31 AM
Subject: Problem using a ConsoleAppender in a Windows Form application
Hi !
I'm using log4net 1.2.10.0 and .NET 2.0
I wrote a Windows Form application and I want to be able to log debug messages in a ColoredConsoleAppender (or whatever type of Console appender).
If I compile my application as a Console Application, this works fine... If I compile it as a Windows Forms application, then I understand that no console is attached to my application, fair enough..
In this case I programmaticaly attach a brand new console by using this piece of code :
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeConsole();
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);
public static void SetConsole()
{
foreach (log4net.Appender.IAppender appender in log4net.LogManager.GetRepository().GetAppenders())
{
System.Type type = appender.GetType();
if ((type == typeof(log4net.Appender.ColoredConsoleAppender))
|| (type == typeof(log4net.Appender.ConsoleAppender)))
{
if (AllocConsole())
AttachConsole(-1);
return;
}
}
So at startup, if any console appender is found, I attach a console...
At this stage, I can do things like : Console.WriteLine() , and it works very fine, I can see the messages in my new console...
But for some reason, the Console Appenders do not forward their messages to this console...
Do I miss something or do something wrong ?
I tried to create a custom appender that inherits ConsoleAppender and do something like:
protected override void Append (LoggingEvent loggingEvent)
{
Console.WriteLine (RenderLoggingEvent(loggingEvent);
}
This works fine with my attached console, but I loose all the nice colored features... And fact is I do want them :)
Thanks a lot for your help
Michael