开发者

Allowing a logger to override a specific appender's parameter?

开发者 https://www.devze.com 2023-01-07 11:27 出处:网络
Using log4net, can I configure a logger so that it can override a single parameter of one of the appenders? Some examples:

Using log4net, can I configure a logger so that it can override a single parameter of one of the appenders? Some examples:

<log4net>
  <appender name="A1" type="log4net.Appender.FileAppender">
    <file value="log4net.log开发者_如何学C">
  </appender>
  <root>
    <level value="ERROR" />
    <appender-ref ref="A1" />
  </root>
  <logger name="MyLogger">
    <level value="WARN" />
    <appender-ref ref="A1">
      <file value="MyLogger.Log"/>
    </appender-ref>
  </logger>
</log4net>

For example, I'd like to configure one of my sub-loggers to send output to a different file from the rest of my application. Am I going the wrong way? Should I configure it with a new appender? What about sending different levels to different appenders? e.g.

<log4net>
  <appender name="default" type="log4net.Appender.FileAppender">
    <file value="My.log">
  </appender>
  <appender name="errors" type="log4net.Appender.FileAppender">
    <file value="Errors.log">
  </appender>
  <root>
    <level value="DEBUG">
      <appender-ref ref="default" />
    </level>
    <level value="ERROR">
      <appender-ref ref="errors" />
    </level>
  </root>
</log4net>


I have added this as a new answer as it is too long to fit in a comment.

Try something like


        <appendToFile value="true" />

        <layout type="log4net.Layout.PatternLayout">
            <header value="[Header]%date&#13;&#10;" />
            <footer value="[Footer]%date&#13;&#10;" />
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
        </layout>
    </appender>

    <appender name="MyLogger" type="log4net.Appender.FileAppender">
        <file type="log4net.Util.PatternString" value="MyLogger.Log" />

        <appendToFile value="true" />

        <layout type="log4net.Layout.PatternLayout">
            <header value="[Header]%date&#13;&#10;" />
            <footer value="[Footer]%date&#13;&#10;" />
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
        </layout>
    </appender>


    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
        <level value="INFO" />
        <appender-ref ref="A1" />
    </root>

    <logger name="SpecialMethod">
        <level value="DEBUG" />
        <appender-ref ref="MyLogger" />
    </logger>

</log4net


I use something similar to your first xml file. (With Log4net for C#)

Don't think the second xml will work, because, as I understand it, it would interpret <level value="DEBUG"> as setting the logging level to DEBUG , whereas you are wanting it to interpret as

  if (Level == DEBUG) ...
0

精彩评论

暂无评论...
验证码 换一张
取 消