2

I have implemented logger for my application and I wanted to log the activity in XML format (XmlLayout) on .NET Core Console Application. The Log4Net configuration includes FileAppender and XmlFileAppender as follows.

<log4net>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
  <file value="C:\Log4NetLogs\UaGatewayText.log.txt"/>
  <layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="[%date{dd.MM.yyyy}] [%date{ABSOLUTE}] [%thread] 
     %level %property{TEST - Ua.Gateway} %message%newline" />

     </layout>
        </appender>
           <appender name="ConsolAppender" 
                            type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%dae{DATE}] [%date{ABSOLUTE}] [%thread] 
                  [%level] %message%newline" />
      </layout>
   </appender>


    <appender name="XmlFileAppender" 
               type="log4net.Appender.RollingFileAppender">
           <file type="log4net.Util.PatternString" 
                value="C:\Log4NetLogs\UaGatewayXml.log.xml" />
        <appendToFile value= "true" />
        <rollingStyle value="size"/>
        <maximumFileSize value ="50MB"/>
        <maxSizeRollBackups value ="10"/>
       <layout type="log4net.Layout.XmlLayout" />
    </appender>


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

   </root>

 </log4net>

In .NET Core Console App, XmlAppender creates UaGatewayXml.log.xml file in the directory and does not write any log. However, it works great with FileAppender.

The above configuration works as expected for .NET Framework (4.5.*) for both XmlFileAppender and FileAppender.

Do I have to make any change in the Config file? Thanks

jack_t
  • 145
  • 3
  • 9
  • Have you [had a look at the log4net debug output](http://haacked.com/archive/2006/09/27/Log4Net_Troubleshooting.aspx/)? – stuartd Jun 20 '17 at 13:25
  • I saw it earlier. .NET Core project does not contain App.config file so I cannot do it. – jack_t Jun 22 '17 at 09:54
  • That's only one way to enable debugging - you can also use `` in your log4net config, or you can do it in code with `log4net.Util.LogLog.InternalDebugging = true;` – stuartd Jun 22 '17 at 09:59

3 Answers3

8

The problem is with log4net.Layout.XmlLayoutSchemaLog4j it uses method writer.WriteStartElement("log4j:event"); which causes validation error.

You can create your own layout (take code from here here )

and just change

writer.WriteStartElement("log4j:event")

into

writer.WriteStartElement("log4j", "event", "log4j");

This method that takes the prefix, element name, and namespace. I have opened the output XML file in YALV and everything works fine. Don't forget to correct all WriteStartElement methods.

I have written this solution here.

Neuron
  • 5,141
  • 5
  • 38
  • 59
  • Thanks! To elaborate a bit on this solution: i.e. you can put this file somewhere into your own asp.net assembly: https://pastebin.com/5rKNDPXf and then reference it in the log4net config using what's described here https://stackoverflow.com/questions/1147103/log4net-xml-output#comment966132_1147774 – Efrain Jun 22 '18 at 12:56
  • Uh.. I had a typo in that pastebin, here's the fixed one: https://pastebin.com/JpN6tc48 – Efrain Jun 22 '18 at 13:14
  • I share the file from onedrive: https://1drv.ms/u/s!AgcSwFZhZyF0iQGp4co6u9OO4F-5 also don't forget to add `` – ozanmut Jan 06 '19 at 23:27
  • Works for me. Just add the line in the log4netConfig – Amir Touitou Jan 05 '20 at 09:40
1

load the config manually:

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
igorc
  • 2,024
  • 2
  • 17
  • 29
  • var collection = XmlConfigurator.Configure(_LoggerRepository, new FileInfo(_Log4netConfigPath)); – jack_t Jun 20 '17 at 12:42
  • As I already explained in the problem I am facing that my logger works for Pattern Layout for .txt file. But I guess theres is some problem with following in .NET Core Framework – jack_t Jun 20 '17 at 14:41
1

I had the same problem with an ASP.Net Core 2.0 web application.

I enabled internal debugging and saw the following:

log4net:ERROR [RollingFileAppender] ErrorCode: GenericFailure. Failed in DoAppend System.ArgumentException: Invalid name character in 'log4j:event'. The ':' character, hexadecimal value 0x3A, cannot be included in a name. at System.Xml.XmlWellFormedWriter.CheckNCName(String ncname) at System.Xml.XmlWellFormedWriter.WriteStartElement(String prefix, String localName, String ns) at log4net.Layout.XmlLayoutSchemaLog4j.FormatXml(XmlWriter writer, LoggingEvent loggingEvent) at log4net.Layout.XmlLayoutBase.Format(TextWriter writer, LoggingEvent loggingEvent) at log4net.Appender.FileAppender.Append(LoggingEvent loggingEvent) at log4net.Appender.AppenderSkeleton.DoAppend(LoggingEvent loggingEvent)

I replaced

<layout type="log4net.Layout.XmlLayoutSchemaLog4j">

           

with

<layout type="log4net.Layout.PatternLayout"> 
  <conversionPattern value="%date{yyyyMMddHHmmss.fff} [%thread] %-5level %logger %appdomain - %class - %exception - %method - %property{GlobalLog4NetProperty} - %property{ThreadProperty} - %property{log4net:HostName} - %message%newline"/>
</layout>

 

Michael Giger
  • 125
  • 12