How can I get log4net to write to a filename based on the class that instantiated the logger?
This is my dilemma. I have a RollingFileAppender
. I have a BufferingForwardingAppender
. The forwarding appender writes to the file appender. My file appender needs a file to write to. I want the file to be named as such %Called Assembly Type%log.txt
, where %Called Assembly Type% is the called assembly type (gasp!)
I'm intending to use this as follows:
Calling from a service called "OkilyDokily
":
var log4NetLogger = LogManager.GetLogger(GetMyAssemblyName());
log4NetLogger.LogInfo("Toodleoo");
Calling from a service called "Neighborinos
":
var log4NetLogger = LogManager.GetLogger(GetMyAssemblyName());
log4NetLogger.LogInfo("Toodleoo");
I expect to have 2 log files, one named OkilyDokilylog.txt
and one named Neighborinoslog.txt
Is there a way to do this in log4net? Is my BufferingForwardingAppender
going to be an issue? I have tried this configuration but %logger
is simply translating to the string logger
:(
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString">
<conversionPattern value="C:\testlogs\%logger log.txt" />
</file>
<appendToFile valu开发者_Python百科e="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="5KB" />
<staticLogFileName value="true" />
<countDirection value="1"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-8level %-21date{M/d/yyyy H:m:ss} %message%newline" />
</layout>
</appender>
Maybe this small tidbit might help you out: you need to override the RollingFileAppender and after modifying the File
property, invoke the ActivateOptions
method, like below:
var myPath = "C:\\";
var log = LogManager.GetLogger(typeof(MySpecificAssembly).Name);
XmlConfigurator.Configure();
var rfa = (RollingFileAppender)LogManager.GetRepository().GetAppenders()
.First(c => c.Name == "RollingFileAppender");
rfa.File = myPath + typeof(MySpecificAssembly).Name + ".log";
rfa.ActivateOptions();
精彩评论