1

I have a log4net logfile appender defined as follows. It works fine. I'd like to put the assembly version of the exe as the file name to better distinguish what version is in play. So, in the example below, I'd like to have ClientAppLog_v1.2.3.4.txt, if the assembly version of my app was 1.2.3.4

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
  <param name="File" value="${USERPROFILE}\MyApp Logs\ClientAppLog.txt" />
  <param name="AppendToFile" value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="Header" value="[Header]&#xD;&#xA;" />
    <param name="Footer" value="[Footer]&#xD;&#xA;" />
    <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
  </layout>
</appender>
Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
  • there is no builtin support for Assembly Name or AssemblyVersion properties, see here how you can solve this with some custom code you have to write your own: http://stackoverflow.com/a/4711419/559144 – Davide Piras Feb 13 '15 at 13:30
  • 1
    Thanks. I don't want to go through all that trouble... I'll probably just manually set the file name at deployment time. Thanks! – Stealth Rabbi Feb 13 '15 at 13:32

1 Answers1

5

While log4net may not support these directly, you can easily do this using a generic log4net property, which you set before creating a logger, or to be on the safe side, before configuring log4net:

Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;

log4net.GlobalContext.Properties["LogFileName"] = version + "_logfile.txt";

log4net.Config.XmlConfigurator.Configure(…);

And in the config replace the <param name="File" with this:

 <file type="log4net.Util.PatternString" 
       value="${USERPROFILE}\MyApp Logs\%property{LogFileName}" />

Result:

Image showing log file in correct location

stuartd
  • 70,509
  • 14
  • 132
  • 163