Logging to different Log4Net loggers with dependency injection
Opened this issue · 1 comments
Hello.
I achieved this :
Using dependency injection, I managed to load log4net confile and to pass ILogger<> to constructors.
It works fine with a root logger and multiple appenders filtering the level, so i.e. it logs DEBUG to a file and ERROR to another.
.ConfigureLogging(logging => { logging.ClearProviders(); logging.SetMinimumLevel(LogLevel.Trace); logging.AddLog4Net("config.log4Net.xml", true);
`
<appender name="MyServiceAppender" type="log4net.Appender.RollingFileAppender">
<encoding type="System.Text.UTF8Encoding" />
<file value="..\Infos.log" />
<appendToFile value="true" />
<maximumFileSize value="5000KB" />
<maxSizeRollBackups value="20" />
<rollingStyle value="Size" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger.%method [%line] > %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<encoding type="System.Text.UTF8Encoding" />
<file value="..\Error.log" />
<appendToFile value="true" />
<maximumFileSize value="10000KB" />
<maxSizeRollBackups value="20" />
<rollingStyle value="Size" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="ERROR" />
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="MyServiceAppender" />
<appender-ref ref="ErrorAppender" />
</root>
`
What I'd like is :
Configure in log4net two loggers, one for the on-the-fly infos, one for the on-the-fly datas.
<appender name="MyServiceAppender" type="log4net.Appender.RollingFileAppender">
<encoding type="System.Text.UTF8Encoding" />
<file value="..\Infos.log" />
<appendToFile value="true" />
<maximumFileSize value="5000KB" />
<maxSizeRollBackups value="20" />
<rollingStyle value="Size" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger.%method [%line] > %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<encoding type="System.Text.UTF8Encoding" />
<file value="..\Error.log" />
<appendToFile value="true" />
<maximumFileSize value="10000KB" />
<maxSizeRollBackups value="20" />
<rollingStyle value="Size" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="ERROR" />
</filter>
</appender>
<appender name="DataAppender" type="log4net.Appender.RollingFileAppender">
<encoding type="System.Text.UTF8Encoding" />
<file value="..\Data.log" />
<appendToFile value="true" />
<maximumFileSize value="10000KB" />
<maxSizeRollBackups value="20" />
<rollingStyle value="Size" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
</appender>
<logger name="MyService">
<level value="ALL" />
<appender-ref ref="MyServiceAppender" />
<appender-ref ref="ErrorAppender" />
</logger>
<logger name="DataService">
<appender-ref ref="DataAppender" />
<level value="DEBUG" />
</logger>
Is there any way to achieve this ?
Thanks for the help.
I found a way to achieve this, with dependency injection.
I write it here, it might be useful for someone else :
I replaced in the constructor's prototype
ILogger<AcquisitionManager> myLogger
by
ILoggerFactory loggerFactory
and added
ILogger myServiceLogger = loggerFactory.CreateLogger("MyService"); ILogger myDataLogger = loggerFactory.CreateLogger("DataService");
I leave it open for now, if you think that's the right way it should be closed.
If there's another way, I'd be glad to hear about it.