简体   繁体   English

使用项目组装信息的log4net文件路径

[英]log4net file path using project Assembly info

I have the following in log4net.config that defines my logger: 我在定义我的记录器的log4net.config中具有以下内容:

  <!-- 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: 有没有一种方法可以使用项目的Assembly.cs信息,从而使我可以构建日志路径,例如:

<file value="${ProgramData}\\${AssemblyCompany}\\${AssemblyProduct}\\log\\Debug" />

You can do this by using log4net's GlobalContext along with a Pattern Layout in the file config. 您可以通过使用log4net的GlobalContext以及文件config中的Pattern Layout来实现。

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 ): 然后在配置中(注意文件类型必须为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 这将评估为C:\\ProgramData\\Company Name\\Product Name\\Log\\Debug\\log.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM