Created
August 27, 2013 01:25
-
-
Save diegocaxito/6348688 to your computer and use it in GitHub Desktop.
Teste simples de geração de logs com log4net (http://logging.apache.org/log4net/)
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration> | |
<configSections> | |
<section name="log4net" | |
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> | |
</configSections> | |
<log4net> | |
<appender name="ExceptionLogFileAppender" type="log4net.Appender.RollingFileAppender"> | |
<file type="log4net.Util.PatternString"> | |
<conversionPattern value="teste.error.log" /> | |
</file> | |
<appendToFile value="true" /> | |
<rollingStyle value="Date" /> | |
<datePattern value="-yyyy-MM-dd" /> | |
<param name="StaticLogFileName" value="false" /> | |
<layout type="log4net.Layout.PatternLayout"> | |
<param name="Footer" value="------------------------------------------ " /> | |
<conversionPattern value="%date [%thread] %level %logger - %message%newline" /> | |
</layout> | |
</appender> | |
<root> | |
<level value="ALL" /> | |
<appender-ref ref="ExceptionLogFileAppender" /> | |
</root> | |
</log4net> | |
</configuration> |
This file contains hidden or 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
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
[assembly: log4net.Config.XmlConfigurator(Watch = true)] | |
namespace Log4NetPoc | |
{ | |
[TestClass] | |
public class Log4NetTeste | |
{ | |
[TestMethod] | |
public void Gerar_apenas_com_excecao() | |
{ | |
Log.Gerar<Log4NetTeste>(new Exception("Erro de teste em Log4Net")); | |
} | |
[TestMethod] | |
public void Gerar_com_excecao_e_objeto_customizado() | |
{ | |
Log.Gerar<Log4NetTeste>(new {mensagem="isso é um teste"}, new Exception("Erro de teste em Log4Net")); | |
} | |
} | |
public static class Log | |
{ | |
public static void Gerar<T>(object mensagem, Exception exception) | |
{ | |
log4net.ILog log = log4net.LogManager.GetLogger(typeof(T)); | |
log.Error(mensagem, exception); | |
} | |
public static void Gerar<T>(Exception exception) | |
{ | |
Gerar<T>(null, exception); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment