BrianKE
BrianKE

Reputation: 4185

log4net file path using project Assembly info

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" />

Upvotes: 2

Views: 1167

Answers (1)

stuartd
stuartd

Reputation: 73253

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

Upvotes: 2

Related Questions