tags:

views:

68

answers:

1

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 value="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>
+1  A: 

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();
code4life
Thank you, although I was hoping there is a way to do this via the configuration file. I'd rather not have to dive into code unless I realllllly need to.
Stefan Valianu
@Stefan: yeah, this is one of those small areas where the configurator just falls short...!
code4life