Deadlock using log4net.Appender.ColoredConsoleAppender












0















Is ColoredConsoleAppender thread safe?



I have a multi-threaded console application where I use the log4net.Appender.ColoredConsoleAppender to get error messages printed in the console window.



I also use the non blocking console reader that is described here: https://stackoverflow.com/a/18342182/1688642



Occasionally the application blocks and pressing the Return-key (Enter) in the console removes the deadlock. This is always followed by an error from log4net (through the ColoredConsoleAppender). I suspect that there is a deadlock between the Console.ReadLine in the console reader and the writing inside the ColoredConsoleAppender (which is not a simple Console.WriteLine).



I have looked at the source code for ColoredConsoleAppender and it was a lot more involved than I thought and I suspect it is not thread safe.



I have also read about the potential deadlock that can happen between Console.ReadLine and Console.WriteLine described here: http://blogs.microsoft.co.il/dorony/2012/09/12/consolereadkey-net-45-changes-may-deadlock-your-system/ but I have concluded that this is not the same problem.



Update 1:
The code below is an illustration, not the real code. And this code does not deadlock....



using System;
using System.IO;
using System.Reflection;
using System.Threading;
using log4net;
using log4net.Config;
...
class Program
{
static void Main(string args)
{
var exeLocation = new FileInfo(Assembly.GetEntryAssembly().Location);
var appConfig = new FileInfo(Path.Combine(exeLocation.DirectoryName, Assembly.GetEntryAssembly().GetName().Name + ".exe.config"));
XmlConfigurator.Configure(appConfig);

// Start two threads that writes log messages
Thread t1 = new Thread(ThreadLoop);
t1.Start("T1");

Thread t2 = new Thread(ThreadLoop);
t2.Start("T2");

ILog log = LogManager.GetLogger("MAIN_LOG");
Console.Write("$ ");
while (true)
{
string line;
if (Reader.TryReadLine(out line, 100))
{
bool handled = ParseAndExecuteCommand(line);
if (!handled)
{
Console.WriteLine("Unknown command (type 'h' to get help).");
}
Console.Write("$ ");
}
log.Info($"Info from main {Environment.TickCount}");
Console.WriteLine($"Console.WriteLine from main {Environment.TickCount}");
}
}

private static void ThreadLoop(object name)
{
while (true)
{
Thread.Sleep(1000);
ILog log = LogManager.GetLogger("THREAD_LOG");
log.Info($"Info from thread {name} {Environment.TickCount}");
log.Warn($"Warning from thread {name} {Environment.TickCount}");
log.Error($"Error from thread {name} {Environment.TickCount}");

Console.WriteLine($"Console.WriteLine from thread {name} {Environment.TickCount}");
}
}


Below is the log4net configuration from App.config:



<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<threshold value="ERROR" />
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>









share|improve this question

























  • You are logging to console in a console app? Is that right? Why not log to a file, then you can tail -f in a different console, without interference. Won't be colored, though.

    – Fildor
    Nov 21 '18 at 11:56











  • You should show a minimal, verifiable example.

    – Crowcoder
    Nov 21 '18 at 11:58











  • I log to text-files and UDP as well. From experience I know that people dont look at the logs very often. If they see a red text in the console window this tends to get their attention.

    – Nils Lande
    Nov 21 '18 at 13:46
















0















Is ColoredConsoleAppender thread safe?



I have a multi-threaded console application where I use the log4net.Appender.ColoredConsoleAppender to get error messages printed in the console window.



I also use the non blocking console reader that is described here: https://stackoverflow.com/a/18342182/1688642



Occasionally the application blocks and pressing the Return-key (Enter) in the console removes the deadlock. This is always followed by an error from log4net (through the ColoredConsoleAppender). I suspect that there is a deadlock between the Console.ReadLine in the console reader and the writing inside the ColoredConsoleAppender (which is not a simple Console.WriteLine).



I have looked at the source code for ColoredConsoleAppender and it was a lot more involved than I thought and I suspect it is not thread safe.



I have also read about the potential deadlock that can happen between Console.ReadLine and Console.WriteLine described here: http://blogs.microsoft.co.il/dorony/2012/09/12/consolereadkey-net-45-changes-may-deadlock-your-system/ but I have concluded that this is not the same problem.



Update 1:
The code below is an illustration, not the real code. And this code does not deadlock....



using System;
using System.IO;
using System.Reflection;
using System.Threading;
using log4net;
using log4net.Config;
...
class Program
{
static void Main(string args)
{
var exeLocation = new FileInfo(Assembly.GetEntryAssembly().Location);
var appConfig = new FileInfo(Path.Combine(exeLocation.DirectoryName, Assembly.GetEntryAssembly().GetName().Name + ".exe.config"));
XmlConfigurator.Configure(appConfig);

// Start two threads that writes log messages
Thread t1 = new Thread(ThreadLoop);
t1.Start("T1");

Thread t2 = new Thread(ThreadLoop);
t2.Start("T2");

ILog log = LogManager.GetLogger("MAIN_LOG");
Console.Write("$ ");
while (true)
{
string line;
if (Reader.TryReadLine(out line, 100))
{
bool handled = ParseAndExecuteCommand(line);
if (!handled)
{
Console.WriteLine("Unknown command (type 'h' to get help).");
}
Console.Write("$ ");
}
log.Info($"Info from main {Environment.TickCount}");
Console.WriteLine($"Console.WriteLine from main {Environment.TickCount}");
}
}

private static void ThreadLoop(object name)
{
while (true)
{
Thread.Sleep(1000);
ILog log = LogManager.GetLogger("THREAD_LOG");
log.Info($"Info from thread {name} {Environment.TickCount}");
log.Warn($"Warning from thread {name} {Environment.TickCount}");
log.Error($"Error from thread {name} {Environment.TickCount}");

Console.WriteLine($"Console.WriteLine from thread {name} {Environment.TickCount}");
}
}


Below is the log4net configuration from App.config:



<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<threshold value="ERROR" />
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>









share|improve this question

























  • You are logging to console in a console app? Is that right? Why not log to a file, then you can tail -f in a different console, without interference. Won't be colored, though.

    – Fildor
    Nov 21 '18 at 11:56











  • You should show a minimal, verifiable example.

    – Crowcoder
    Nov 21 '18 at 11:58











  • I log to text-files and UDP as well. From experience I know that people dont look at the logs very often. If they see a red text in the console window this tends to get their attention.

    – Nils Lande
    Nov 21 '18 at 13:46














0












0








0








Is ColoredConsoleAppender thread safe?



I have a multi-threaded console application where I use the log4net.Appender.ColoredConsoleAppender to get error messages printed in the console window.



I also use the non blocking console reader that is described here: https://stackoverflow.com/a/18342182/1688642



Occasionally the application blocks and pressing the Return-key (Enter) in the console removes the deadlock. This is always followed by an error from log4net (through the ColoredConsoleAppender). I suspect that there is a deadlock between the Console.ReadLine in the console reader and the writing inside the ColoredConsoleAppender (which is not a simple Console.WriteLine).



I have looked at the source code for ColoredConsoleAppender and it was a lot more involved than I thought and I suspect it is not thread safe.



I have also read about the potential deadlock that can happen between Console.ReadLine and Console.WriteLine described here: http://blogs.microsoft.co.il/dorony/2012/09/12/consolereadkey-net-45-changes-may-deadlock-your-system/ but I have concluded that this is not the same problem.



Update 1:
The code below is an illustration, not the real code. And this code does not deadlock....



using System;
using System.IO;
using System.Reflection;
using System.Threading;
using log4net;
using log4net.Config;
...
class Program
{
static void Main(string args)
{
var exeLocation = new FileInfo(Assembly.GetEntryAssembly().Location);
var appConfig = new FileInfo(Path.Combine(exeLocation.DirectoryName, Assembly.GetEntryAssembly().GetName().Name + ".exe.config"));
XmlConfigurator.Configure(appConfig);

// Start two threads that writes log messages
Thread t1 = new Thread(ThreadLoop);
t1.Start("T1");

Thread t2 = new Thread(ThreadLoop);
t2.Start("T2");

ILog log = LogManager.GetLogger("MAIN_LOG");
Console.Write("$ ");
while (true)
{
string line;
if (Reader.TryReadLine(out line, 100))
{
bool handled = ParseAndExecuteCommand(line);
if (!handled)
{
Console.WriteLine("Unknown command (type 'h' to get help).");
}
Console.Write("$ ");
}
log.Info($"Info from main {Environment.TickCount}");
Console.WriteLine($"Console.WriteLine from main {Environment.TickCount}");
}
}

private static void ThreadLoop(object name)
{
while (true)
{
Thread.Sleep(1000);
ILog log = LogManager.GetLogger("THREAD_LOG");
log.Info($"Info from thread {name} {Environment.TickCount}");
log.Warn($"Warning from thread {name} {Environment.TickCount}");
log.Error($"Error from thread {name} {Environment.TickCount}");

Console.WriteLine($"Console.WriteLine from thread {name} {Environment.TickCount}");
}
}


Below is the log4net configuration from App.config:



<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<threshold value="ERROR" />
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>









share|improve this question
















Is ColoredConsoleAppender thread safe?



I have a multi-threaded console application where I use the log4net.Appender.ColoredConsoleAppender to get error messages printed in the console window.



I also use the non blocking console reader that is described here: https://stackoverflow.com/a/18342182/1688642



Occasionally the application blocks and pressing the Return-key (Enter) in the console removes the deadlock. This is always followed by an error from log4net (through the ColoredConsoleAppender). I suspect that there is a deadlock between the Console.ReadLine in the console reader and the writing inside the ColoredConsoleAppender (which is not a simple Console.WriteLine).



I have looked at the source code for ColoredConsoleAppender and it was a lot more involved than I thought and I suspect it is not thread safe.



I have also read about the potential deadlock that can happen between Console.ReadLine and Console.WriteLine described here: http://blogs.microsoft.co.il/dorony/2012/09/12/consolereadkey-net-45-changes-may-deadlock-your-system/ but I have concluded that this is not the same problem.



Update 1:
The code below is an illustration, not the real code. And this code does not deadlock....



using System;
using System.IO;
using System.Reflection;
using System.Threading;
using log4net;
using log4net.Config;
...
class Program
{
static void Main(string args)
{
var exeLocation = new FileInfo(Assembly.GetEntryAssembly().Location);
var appConfig = new FileInfo(Path.Combine(exeLocation.DirectoryName, Assembly.GetEntryAssembly().GetName().Name + ".exe.config"));
XmlConfigurator.Configure(appConfig);

// Start two threads that writes log messages
Thread t1 = new Thread(ThreadLoop);
t1.Start("T1");

Thread t2 = new Thread(ThreadLoop);
t2.Start("T2");

ILog log = LogManager.GetLogger("MAIN_LOG");
Console.Write("$ ");
while (true)
{
string line;
if (Reader.TryReadLine(out line, 100))
{
bool handled = ParseAndExecuteCommand(line);
if (!handled)
{
Console.WriteLine("Unknown command (type 'h' to get help).");
}
Console.Write("$ ");
}
log.Info($"Info from main {Environment.TickCount}");
Console.WriteLine($"Console.WriteLine from main {Environment.TickCount}");
}
}

private static void ThreadLoop(object name)
{
while (true)
{
Thread.Sleep(1000);
ILog log = LogManager.GetLogger("THREAD_LOG");
log.Info($"Info from thread {name} {Environment.TickCount}");
log.Warn($"Warning from thread {name} {Environment.TickCount}");
log.Error($"Error from thread {name} {Environment.TickCount}");

Console.WriteLine($"Console.WriteLine from thread {name} {Environment.TickCount}");
}
}


Below is the log4net configuration from App.config:



<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<threshold value="ERROR" />
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>






c# console log4net log4net-appender






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 16:09







Nils Lande

















asked Nov 21 '18 at 11:52









Nils LandeNils Lande

51658




51658













  • You are logging to console in a console app? Is that right? Why not log to a file, then you can tail -f in a different console, without interference. Won't be colored, though.

    – Fildor
    Nov 21 '18 at 11:56











  • You should show a minimal, verifiable example.

    – Crowcoder
    Nov 21 '18 at 11:58











  • I log to text-files and UDP as well. From experience I know that people dont look at the logs very often. If they see a red text in the console window this tends to get their attention.

    – Nils Lande
    Nov 21 '18 at 13:46



















  • You are logging to console in a console app? Is that right? Why not log to a file, then you can tail -f in a different console, without interference. Won't be colored, though.

    – Fildor
    Nov 21 '18 at 11:56











  • You should show a minimal, verifiable example.

    – Crowcoder
    Nov 21 '18 at 11:58











  • I log to text-files and UDP as well. From experience I know that people dont look at the logs very often. If they see a red text in the console window this tends to get their attention.

    – Nils Lande
    Nov 21 '18 at 13:46

















You are logging to console in a console app? Is that right? Why not log to a file, then you can tail -f in a different console, without interference. Won't be colored, though.

– Fildor
Nov 21 '18 at 11:56





You are logging to console in a console app? Is that right? Why not log to a file, then you can tail -f in a different console, without interference. Won't be colored, though.

– Fildor
Nov 21 '18 at 11:56













You should show a minimal, verifiable example.

– Crowcoder
Nov 21 '18 at 11:58





You should show a minimal, verifiable example.

– Crowcoder
Nov 21 '18 at 11:58













I log to text-files and UDP as well. From experience I know that people dont look at the logs very often. If they see a red text in the console window this tends to get their attention.

– Nils Lande
Nov 21 '18 at 13:46





I log to text-files and UDP as well. From experience I know that people dont look at the logs very often. If they see a red text in the console window this tends to get their attention.

– Nils Lande
Nov 21 '18 at 13:46












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411476%2fdeadlock-using-log4net-appender-coloredconsoleappender%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411476%2fdeadlock-using-log4net-appender-coloredconsoleappender%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]