问题
My code is :
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="MyLog" type="log4net.Appender.RollingFileAppender">
<file value="logs\log-file.txt" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="MyWayLog" />
</root>
</log4net>
</configuration>
LoggerInstaler.cs
public class LoggerInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithConfig("log4net.config"));
}
}
MyLogger.cs
private static ILoggerFactory loggera = IoC.Container.Resolve<ILoggerFactory>();
private static ILogger logger = loggera.Create("MyLog");
public static ILogger Log
{
get { return logger; }
set { logger = value; }
}
When I Use Log.Error("some exception")
In log file I have other logs from some other dll. I want only my logs, not the other dll exception, only explicitly from call (Log.Error).
回答1:
You have to use
<logger name="MyLog">
instead of
<root>
in the log4net.config
You may also use ToLog method within windsor regitration
container.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithConfig("log4net.config").ToLog("MyLog"));
Not sure about your MyLogger.cs: Due to windsor facility, there's should no need for that...
simply set ILogger
as dependency when you need it
来源:https://stackoverflow.com/questions/18186595/logging-with-castle-windsor-the-logging-facility-and-log4net