简体   繁体   中英

log4net Colored Console Appender issue

I am using log4net library for showing logs on my console , I am able to show logs on console when am running the application in debug mode, but when I am running the console through a cmd, it is not showing the logs on console.

XML Config :

<?xml version="1.0" encoding="utf-8"?>
<log4net>
<appender name="RollingFile" type="AutoLogger.FileAppender">
<file type="log4net.Util.PatternString" value="%property{LogFileName}" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maximumFileSize value="200MB" />
<maxSizeRollBackups value="-1" />
<datePattern value="dd.MM.yyyy'.log'" />
<layout type="AutoLogger.TestcasePatternLayout, AutoLogger">
  <conversionPattern value="%level %date %testcase %message (%logger)%newline" />
  <param name="Header" value="[TEST START]&#xD;&#xA;" />
  <param name="Footer" value="[TEST END]&#xD;&#xA;" />
</layout>
</appender>

<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
  <level value="ERROR" />
  <foreColor value="White" />
  <backColor value="Red" />
</mapping>
<mapping>
  <level value="WARN" />
  <foreColor value="Yellow" />
</mapping>
<mapping>
  <level value="FATAL" />
  <foreColor value="White" />
  <backColor value="Red, HighIntensity" />
</mapping>
<layout type="AutoLogger.TestcasePatternLayout, AutoLogger">
  <conversionPattern value="%level %testcase %message %newline" />
</layout>
</appender>
<root>
<priority value="ALL" />
<appender-ref ref="ColoredConsoleAppender" />
<appender-ref ref="RollingFile" />
</root>
</log4net>

I suspect that I know this one...

You posted some of the XML configuration for log4net, but there is one important bit of info you didn't mention. If you are targeting dotnet core, I can help you out. If you are still targeting the dontnet framework, well, all I can suggest is go back and double check all the online tutorials. I have used the log4net colored console appender for years under the dontnet framework without fail, it works just fine.

On to the main event: If you are working with dotnet core applications, all the other log4net appenders work just fine without any changes from the posted docs and tutorials. For some reason, though, the colored console appender needed to be changed. I dredged this up after quite a bit of digging in the Internet:

  <appender name="ColoredConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender">
    <threshold value="ALL" />
    <mapping>
      <level value="FATAL" />
      <foreColor value="Magenta" />
    </mapping>
    <mapping>
      <level value="ERROR" />
      <foreColor value="Red" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <foreColor value="Yellow" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <foreColor value="Green" />
    </mapping>
    <mapping>
      <level value="DEBUG" />
      <foreColor value="Cyan" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>

  <root>
    <level value="ALL" />
    <appender-ref ref="ColoredConsoleAppender" />
  </root>

Just add this to your configuration file as you would any other appender, and it will work just fine.

Bonus points! Want to define your own set of colors? You cannot use the color names that are from the original colored console appender, it will break. Here is a link to the enumerated values that are legal for the ManagedColoredConsoleAppender:

https://docs.microsoft.com/en-us/dotnet/api/system.consolecolor?redirectedfrom=MSDN&view=net-5.0

May all your logged events be green!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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