Log4Net, how to add a custom field to my logging
https://www.orcode.com/question/1116824_k1eb72.html
LogicalThreadContext Class
The LogicalThreadContext provides a location for CallContext specific debugging information to be stored. The LogicalThreadContext properties override any ThreadContext or GlobalContext properties with the same name.
For .NET Standard 1.3 this class uses System.Threading.AsyncLocal rather than CallContext.
The Logical Thread Context has a properties map and a stack. The properties and stack can be included in the output of log messages. The PatternLayout supports selecting and outputting these properties.
The Logical Thread Context provides a diagnostic context for the current call context. This is an instrument for distinguishing interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.
The Logical Thread Context is managed on a per CallContext basis.
The CallContext requires a link time SecurityPermission for the Infrastructure. If the calling code does not have this permission then this context will be disabled. It will not store any property values set on it.
ThreadContext Class
The ThreadContext provides a location for thread specific debugging information to be stored. The ThreadContext properties override any GlobalContext properties with the same name.
The thread context has a properties map and a stack. The properties and stack can be included in the output of log messages. The PatternLayout supports selecting and outputting these properties.
The Thread Context provides a diagnostic context for the current thread. This is an instrument for distinguishing interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.
The Thread Context is managed on a per thread basis.
GlobalContext Class
The GlobalContext provides a location for global debugging information to be stored.
The global context has a properties map and these properties can be included in the output of log messages. The PatternLayout supports selecting and outputing these properties.
By default the log4net:HostName property is set to the name of the current machine.
wcf使用遇到的问题
https://issues.apache.org/jira/browse/LOG4NET-398
https://stackoverflow.com/questions/334367/when-to-use-nested-diagnostic-context-ndc
Log4Net, how to add a custom field to my logging
回答1
1) Modify the command text: INSERT INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MyColumn]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @CustomColumn)
2) Add the parameter definition for the custom column:
<parameter>
<parameterName value="@CustomColumn"/>
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{CustomColumn}" />
</layout>
</parameter>
3) Then use one of log4net’s contexts to transfer values to the parameter:
// thread properties...
log4net.LogicalThreadContext.Properties["CustomColumn"] = "Custom value";
log.Info("Message");
// ...or global properties
log4net.GlobalContext.Properties["CustomColumn"] = "Custom value";
回答2
Three types of logging context available in Log4Net.
-
Log4Net.GlobalContext :- This context shared across all application threads and domains.If two threads set the same property on GlobalContext, One Value will override the other.
-
Log4Net.ThreadContext :- This context scope limited to calling thread. Here two threads can set same property to different values without overriding to each other.
-
Log4Net.ThreadLogicalContext :- This context behaves similarly to the ThreadContext. if you're working with a custom thread pool algorithm or hosting the CLR, you may find some use for this one.
Add the following code to your program.cs file:
static void Main( string[] args )
{
log4net.Config.XmlConfigurator.Configure();
log4net.ThreadContext.Properties[ "myContext" ] = "Logging from Main";
Log.Info( "this is an info message" );
Console.ReadLine();
}
2) Add the parameter definition for the custom column:
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger (%property{myContext}) [%level]- %message%newline" />
</layout>
</appender>
</log4net>
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了