C# 为同步日志记录继承NLog WrapperTargetBase
我有一个.NET Core 3.1 Web服务,并使用NLog 4.7.0进行日志记录。我目前正在使用文件日志和filebeat将日志文件中的消息发送给ELK 我想为我的C# 为同步日志记录继承NLog WrapperTargetBase,c#,nlog,C#,Nlog,我有一个.NET Core 3.1 Web服务,并使用NLog 4.7.0进行日志记录。我目前正在使用文件日志和filebeat将日志文件中的消息发送给ELK 我想为我的FileTarget创建一个包装器目标。实际目的是将单个web请求的所有日志合并到单个日志消息中,但这可能不是重点 我想从NLog.Targets.Wrappers.WrapperTargetBase继承,但是Write(LogEventInfo logEvent)方法是密封的。这是否意味着我只能对包装器目标使用异步日志记录 这
FileTarget
创建一个包装器目标。实际目的是将单个web请求的所有日志合并到单个日志消息中,但这可能不是重点
我想从NLog.Targets.Wrappers.WrapperTargetBase
继承,但是Write(LogEventInfo logEvent)
方法是密封的。这是否意味着我只能对包装器目标使用异步日志记录
这是否会影响我如何设置日志配置?注意,我使用的是配置文件,而不是配置API
我现有的FileTarget
如下所示:
"appFile": {
"type": "File",
"fileName": "c:\\wwwlogs\\MyWebApp\\Combined.log",
"archiveFileName": "c:\\wwwlogs\\MyWebApp.Web\\archives\\Combined.{#}.log",
"archiveEvery": "Day",
"archiveNumbering": "Rolling",
"maxArchiveFiles": "7",
"layout": "${longdate}|${aspnet-request-ip}|${aspnet-TraceIdentifier}|${aspnet-user-identity}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
}```
您有一些选择:
在所有情况下,您都需要类中的属性:
[Target("MyWrapper", IsWrapper = true)]
/// <summary>
/// Gets or sets the target that is wrapped by this target.
/// </summary>
[RequiredParameter]
public Target WrappedTarget { get; set; }
包装器TargetBase单个项目
从WrapperTargetBase继承时,目标可以是异步的,但也可以是同步的。你需要更多的代码来处理它
WrappedTarget.writeAsynClogeEvent(logEvent)写入目标代码>
writeAsynClogeEvent
时,请不要调用logEvent.Continuation
(异常或空)
包装器targetbase已分组
写入事件分组
受保护的覆盖无效写入(IList logEvents)
asynchHelpers
[Target(“MyWrapper”,IsWrapper=true)]
,所以这是可行的。你在课堂上需要这个:
[Target("MyWrapper", IsWrapper = true)]
/// <summary>
/// Gets or sets the target that is wrapped by this target.
/// </summary>
[RequiredParameter]
public Target WrappedTarget { get; set; }
//
///获取或设置由此目标包装的目标。
///
[必需参数]
公共目标WrappedTarget{get;set;}