简体   繁体   English

Nlog 可以针对 Azure Log Analytics 吗?

[英]Can Nlog target Azure Log Analytics?

I am writing a web api in .NET 6. I don't seem to find a sample where I can write to custom tables under Azure Log Analytics .我正在 .NET 6 中编写 Web API。我似乎找不到可以写入Azure Log Analytics下的自定义表的示例。 They can be written to via posting a json payload, I was hoping there would be a nuget package library that can already do that taking a workspaceId and sharedkey for access.可以通过发布 json 有效负载来写入它们,我希望有一个 nuget 包库已经可以通过 workspaceId 和 sharedkey 进行访问。

Azure Log Analytics is compatible with the following frameworks .NET Core, .NET 5 and .NET 6. Azure Log Analytics 与以下框架兼容:.NET Core、.NET 5 和 .NET 6。

To write custom data collections to Log Analytics using wrapper使用包装器将自定义数据集合写入 Log Analytics

Install-Package LogAnalytics.Client安装包 LogAnalytics.Client

LogAnalytics_Wrapper logger = new LogAnalytics_Wrapper(
                workspaceId: "Workspace id,
                sharedKey: "Shared key");
                
await logger.SendLogEntry(new SomeEntity
{
    Category = GetCategory(),
    SomeString = $"String Test",
    SomeBoolean = true,
    SomeDateTime = DateTime.UtcNow,
    SomeDouble = 2.1,
    SomeGuid = Guid.NewGuid()
}, "testlog")
.ConfigureAwait(false);  ConfigureAwait(false) 

Log entries using wrapper使用包装器的日志条目

LogAnalyticsClient logger = new LogAnalyticsClient(
                workspaceId: "Workspace id",
                sharedKey: "Shared KEY");
List<DemoEntity> entities = new List<DemoEntity>();
for (int i = 0; i < 1000; ii++)
{
    entities.Add(new DemoEntity
    {
        Criticality = GetCriticality(),
        Message = "Message",
        SystemSource = GetSystemSource()
    });
}
await logger.SendLogEntries(entities, "testlog").ConfigureAwait(false)

We can fetch the workspaceId and shared key values from appsettings.json or KeyVault我们可以从 appsettings.json 或 KeyVault 获取 workspaceId 和共享键值
在此处输入图像描述

Fetching the logs from azure using the below command使用以下命令从 azure 中获取日志

search *
| where Type == "testlog_CL"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM