Created
April 2, 2014 12:15
-
-
Save nillfm/9932944 to your computer and use it in GitHub Desktop.
log4net verbose exception logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration> | |
<configSections> | |
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> | |
</configSections> | |
<log4net> | |
<appender name="SuperConsole" type="log4net.Appender.ConsoleAppender"> | |
<target value="Console.Error" /> | |
<layout type="log4net.Layout.PatternLayout"> | |
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message %exception %newline"/> | |
</layout> | |
</appender> | |
<root> | |
<priority value="ALL"/> | |
<appender-ref ref="SuperConsole"/> | |
</root> | |
<renderer renderingClass="l4n.FullExceptionRenderer" renderedClass="System.Exception"/> | |
</log4net> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace l4n | |
{ | |
public class FullExceptionRenderer : IObjectRenderer | |
{ | |
public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer) | |
{ | |
Exception e = obj as Exception; | |
if (e != null) | |
{ | |
// log what we want from the exception | |
writer.Write("CUSTOM RENDER is saying Message=\"{0}\", Stack = {1}", e.Message, e.StackTrace); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
Exception e; | |
try | |
{ | |
throw new InvalidOperationException("Can't touch this."); | |
} | |
catch (Exception ex) | |
{ | |
LogManager.GetLogger(typeof(Program)).Error("Caught exception", ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment