gpt4 book ai didi

asp.net - 当我使用 '?' 模式时,log4net 写入 `%type, %method and %line`

转载 作者:行者123 更新时间:2023-12-02 09:07:57 25 4
gpt4 key购买 nike

我正在尝试在我的 c# ASP.net Core 2.2 应用程序中使用 log4net,但是当我使用这些模式中的任何一个时,它们都会给我带问号的输出。

我的 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>

写入日志后文件如下所示:
2019-04-17 11:48:44,230 | INFO | [1] | ? ? ? - abc

知道为什么会发生这种情况以及如何解决这个问题吗?
我看到了一些使用这些模式的教程,并试图从中复制,但结果是一样的......也许我的配置有问题?

来自 here我知道我可以将文件名和行添加到我的消息中,但我想先在配置文件中尝试修复。

谢谢。

最佳答案

我将您的配置复制粘贴到我的项目中,它按预期工作。你是在 Release 还是 Debug 中运行你的项目?你读过这些模式的限制吗?

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.



编辑

在 .net core/.net 标准 StackTrace 中不完全支持:

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

所以 className , fileName , lineNumber , methodName , fullInfo不可用。

关于asp.net - 当我使用 '?' 模式时,log4net 写入 `%type, %method and %line`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55724516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com