.Net?Core?使用?TagProvider?與?Enricher?豐富日志的操作代碼
TagProvider
[LogProperties] 與 [LogPropertyIgnore] 如果用在DTO不存在任何問(wèn)題,如果用在Domain實(shí)體上,可能有點(diǎn)混亂。
您可能不希望因日志記錄問(wèn)題而使您的域模型變得混亂。對(duì)于這種情況,可以使用[TagProvider]屬性來(lái)豐富日志。
我們?nèi)匀皇褂们懊嬗玫腘etwork實(shí)體,這次它不再使用[LogPropertyIgnore]屬性:
public class NetWorkInfo
{
public string IPAddress { get; set; }
public int Port { get; set; }
}相反,我們定義了一個(gè)專(zhuān)用的“TagProvider”實(shí)現(xiàn)。
不需要實(shí)現(xiàn)接口或任何類(lèi),只需要正確的方法格式。
下面是我們給Network對(duì)象的標(biāo)簽提供程序,我們只記錄字段IPAddres字段,如下所示:
internal static class NetWorkInfoTagProvider
{
// ?? Has the required signature 'void RecordTags(ITagCollector, T)'
public static void RecordTags(ITagCollector collector, NetWorkInfo network)
{
// You can add aribrtrary objects to be logged.
// You provide a key (first arg) and a value.
collector.Add(nameof(NetWorkInfo.IPAddress), network.IPAddress);
}
}定義標(biāo)簽提供程序后,我們現(xiàn)在可以在日志記錄方法中使用它。
將屬性替換[LogProperties]為[TagProvider]如下所示的屬性:
public static partial class Log
{
[LoggerMessage(
EventId = 0,
Level = LogLevel.Error,
Message = "Can not open SQL connection {err}")]
public static partial void CouldNotOpenConnection(this ILogger logger, string err,
[TagProvider(typeof(NetWorkInfoTagProvider), nameof(NetWorkInfoTagProvider.RecordTags))] NetWorkInfo netWork);
}按正常方式調(diào)用即可,可以看到Network.IPAddress已經(jīng)記錄到日志的State屬性中。
private static async Task Main(string[] args)
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(
builder =>
builder.AddJsonConsole(
options =>
options.JsonWriterOptions = new JsonWriterOptions()
{
Indented = true
}));
ILogger logger = loggerFactory.CreateLogger("Program");
logger.CouldNotOpenConnection("network err", new NetWorkInfo { IPAddress = "123.1.1", Port = 7777 });
}
Enricher
Microsoft.Extensions.Telemetry包可以像Serilog一樣豐富日志。首先添加Nuget包
<PackageReference Include="Microsoft.Extensions.Telemetry" Version="8.3.0" />
首先使用方法ILoggingBuilder.EnableEnrichment()啟用全局豐富,并通過(guò)AddProcessLogEnricher將進(jìn)程的日志信息添加到日志中。
builder.Logging.AddJsonConsole(options =>
options.JsonWriterOptions = new JsonWriterOptions()
{
Indented = true
}
);
builder.Logging.EnableEnrichment(); // Enable log enrichment
builder.Services.AddProcessLogEnricher(x =>
{
x.ProcessId = true; // Add the process ID (true by default)
x.ThreadId = true; // Add the managed thread ID (false by default)
});也可以通過(guò)metadata自定義使用的字段
builder.Services.AddServiceLogEnricher(options =>
{
options.ApplicationName = true; // Choose which values to add to the logs
options.BuildVersion = true;
options.DeploymentRing = true;
options.EnvironmentName = true;
});
builder.Services.AddApplicationMetadata(x =>
{
x.ApplicationName = "My App";
x.BuildVersion = "1.2.3";
x.EnvironmentName = "Development";
x.DeploymentRing = "test";
});
這些內(nèi)置的豐富器很方便,但也可以創(chuàng)建自定義的實(shí)現(xiàn)。
自定義LogEnricher
您可以通過(guò)從或接口IStaticLogEnricher和ILogEnricher派生創(chuàng)建自己的豐富器
- IStaticLogEnricher: IStaticLogEnricher—是全局的enricher,如果日志在整個(gè)聲明周期中不變則可將其標(biāo)簽添加到記錄器中。
- ILogEnricher- 每次寫(xiě)入日志時(shí)都會(huì)調(diào)用豐富器,這對(duì)于可能更改的值非常有用。
我們將創(chuàng)建一個(gè)簡(jiǎn)單的IStaticLogEnricher,將當(dāng)前計(jì)算機(jī)名稱(chēng)添加到日志中,另外創(chuàng)建一個(gè)ILogEnricher,將當(dāng)前用戶(hù)Id添加到日志中。
internal class MachineNameEnricher : IStaticLogEnricher
{
public void Enrich(IEnrichmentTagCollector collector)
{
collector.Add("MachineName", Environment.MachineName);
}
}
internal class UserIdEnricher : ILogEnricher
{
public void Enrich(IEnrichmentTagCollector collector)
{
collector.Add("UserId", Guid.NewGuid().ToString());
}
}
builder.Logging.EnableEnrichment(); // Enable log enrichment
builder.Services.AddStaticLogEnricher<MachineNameEnricher>();
builder.Services.AddLogEnricher<UserIdEnricher>();
到此這篇關(guān)于.Net Core 使用 TagProvider 與 Enricher 豐富日志的文章就介紹到這了,更多相關(guān).Net Core 使用 TagProvider 與 Enricher內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
HTTP錯(cuò)誤500.19解決方法(定義了重復(fù)的節(jié)點(diǎn))
HTTP 錯(cuò)誤 500.19 - Internal Server Error 無(wú)法訪(fǎng)問(wèn)請(qǐng)求的頁(yè)面,因?yàn)樵擁?yè)的相關(guān)配置數(shù)據(jù)無(wú)效2013-06-06
asp.net SAF 中緩存服務(wù)的實(shí)現(xiàn)
對(duì)緩存的興趣源于張子陽(yáng)寫(xiě)的一篇文章《SAF 中緩存服務(wù)的實(shí)現(xiàn)》中的一個(gè)例子:2008-08-08
Linux下部署.net core環(huán)境的步驟詳解
這篇文章主要給大家介紹了在Linux下部署.net core環(huán)境的步驟,文中給出了詳細(xì)的介紹,相信對(duì)大家的學(xué)習(xí)或者工作具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04
Asp.net 2.0 無(wú)刷新圖片上傳 顯示縮略圖 具體實(shí)現(xiàn)
簡(jiǎn)單三步實(shí)現(xiàn)圖片無(wú)刷新上傳:注意是上傳,至于上傳時(shí)的驗(yàn)證,比如圖片的尺寸,大小,格式。自行解決。如果我搞定了,也會(huì)貼上來(lái)的。2013-06-06
asp.net簡(jiǎn)單實(shí)現(xiàn)頁(yè)面換膚的方法
這篇文章主要介紹了asp.net簡(jiǎn)單實(shí)現(xiàn)頁(yè)面換膚的方法,可實(shí)現(xiàn)給頁(yè)面動(dòng)態(tài)添加樣式的功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12
asp.net下模態(tài)對(duì)話(huà)框關(guān)閉之后繼續(xù)執(zhí)行服務(wù)器端代碼的問(wèn)題
asp.net下模態(tài)對(duì)話(huà)框關(guān)閉之后繼續(xù)執(zhí)行服務(wù)器端代碼的問(wèn)題...2007-04-04
ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)
這篇文章介紹了ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
ASP.NET中UpdatePanel與jQuery同時(shí)使用所遇問(wèn)題解決
在.NET中使用了UpdatePanel,里面的輸入框使用了jQuery的日歷選擇器,接下來(lái)介紹下兩者同時(shí)使用的一些細(xì)節(jié)及問(wèn)題的解決方法,感興趣的各位可以參考下哈2013-03-03

