4

I'm trying to use the log4net in my c# ASP.net Core 2.2 app but when I use any of these patterns they give me output with question marks.

My log4net.config:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="C:\Temp\app.log" />
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date | %level | [%thread] | %type %method %line - %message%n" />
    </layout>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="RollingFile" />
  </root>
</log4net>

the file looks like this after a log is written:

2019-04-17 11:48:44,230 | INFO | [1] | ? ? ? - abc

any idea why this is happening and how to fix this? I saw some tutorials that use these patterns and tried to copy from them but the results were the same... maybe something in my configuration is wrong?

from here I understand that I can just add the file name and line to my message but I wanted to try a fix in the config file first.

thanks.

Dzak
  • 412
  • 4
  • 17

1 Answers1

6

I copy-paste your configuration to my project and it works as expected. Did you run your project in Release or Debug? Did you read about limitations in those patterns?

Note about caller location information. The following patterns %type %file %line %method %location %class %C %F %L %l %M all generate caller location information. Location information uses the System.Diagnostics.StackTrace class to generate a call stack. The caller's information is then extracted from this stack.

The System.Diagnostics.StackTrace class is not supported on the .NET Compact Framework 1.0 therefore caller location information is not available on that framework.

The System.Diagnostics.StackTrace class has this to say about Release builds:

StackTrace information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects. StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization.

This means that in a Release build the caller information may be incomplete or may not exist at all! Therefore caller location information cannot be relied upon in a Release build.

Edit

In .net core / .net standard StackTrace isn't fully supported:

https://github.com/apache/logging-log4net/blob/master/src/Core/LocationInfo.cs#L86

So className, fileName, lineNumber, methodName, fullInfo are not available.

Peska
  • 3,980
  • 3
  • 23
  • 40
  • updated my question, I`m using ASP.net core 2.2 and I'm running my project in debug – Dzak Apr 17 '19 at 10:41