2

I have the following in log4net.config that defines my logger:

  <!-- Setup Rolling Log File to log all information -->
  <appender name="DebugFileAppender" type="log4net.Appender.RollingFileAppender" >
    <file value="${ProgramData}\\My Company\\My Product\\log\\Debug" />
    <appendToFile value="true"/>
    <rollingStyle value="Date"/>
    <datePattern value="_yyyy-MM.\tx\t"/>
    <staticLogFileName value="false"/>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %-5level - %message%newline" />
    </layout>
  </appender>

Is there a way to use the project's Assembly.cs info that would allow me to build the log path like:

<file value="${ProgramData}\\${AssemblyCompany}\\${AssemblyProduct}\\log\\Debug" />
stuartd
  • 70,509
  • 14
  • 132
  • 163
BrianKE
  • 4,035
  • 13
  • 65
  • 115

1 Answers1

2

You can do this by using log4net's GlobalContext along with a Pattern Layout in the file config.

So before configuring you set the values (there's an example of getting them from the AssemblyInfo here)

// Properties must be set before configuration

log4net.GlobalContext.Properties["Company"] = "Company Name";
log4net.GlobalContext.Properties["Product"] = "Product Name";

log4net.Config.XmlConfigurator.Configure(…);

Then in the config (note the file type must be PatternString):

 <file type="log4net.Util.PatternString" 
       value="${ProgramData}\%property{Company}\%property{Product}\Log\Debug\log.log" />

This would evaluate to C:\ProgramData\Company Name\Product Name\Log\Debug\log.log

Community
  • 1
  • 1
stuartd
  • 70,509
  • 14
  • 132
  • 163