C#中Azure功能的Application Insight Logging

Dev*_*son 0 c# azure azure-application-insights azure-functions azure-functions-runtime

我们在C#中具有服务总线触发的Azure函数。我想在Application Insight中记录信息(例如调用的函数,结果,异常)。我已将TraceWriter转换为Ilogger以获取登录信息。我要实现的是在控制台(本地)以及Application Insight实例上进行日志记录。什么是实现此目标的完美方法?

    public static class AIFunction
{
    private static string key = TelemetryConfiguration.Active.InstrumentationKey = "************************";

    private static TelemetryClient telemetryClient =
        new TelemetryClient() { InstrumentationKey = key };

    [FunctionName("AIFunction")]
    public static void Run([ServiceBusTrigger("AIFunctionQueue", AccessRights.Manage, Connection = "ServiceBus")]string queueItem, ILogger log)
    {
        telemetryClient.Context.Cloud.RoleName = "AIFunction";
        log.LogInformation($"C# ServiceBus queue trigger function processed message: {queueItem}");
        log.LogInformation($"C# ServiceBus queue trigger function to test Application Insight Logging");
        telemetryClient.TrackEvent("AIFunction TrackEvent");
        telemetryClient.TrackTrace("AIFunction TrackTrace");
    }
}
Run Code Online (Sandbox Code Playgroud)

kam*_*lod 5

您可以TraceWriter额外注入:

private static string key = TelemetryConfiguration.Active.InstrumentationKey = "";

private static TelemetryClient telemetryClient =
    new TelemetryClient() { InstrumentationKey = key };

[FunctionName("Function1")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req,
    ILogger log,
    TraceWriter writer)
{
    telemetryClient.Context.Cloud.RoleName = "AIFunction";
    log.LogInformation($"C# ServiceBus queue trigger function processed message: ");
    log.LogInformation($"C# ServiceBus queue trigger function to test Application Insight Logging");
    writer.Info("C# ServiceBus queue trigger function processed message: ");
    writer.Info("C# ServiceBus queue trigger function to test Application Insight Logging");
    telemetryClient.TrackEvent("AIFunction TrackEvent");
    telemetryClient.TrackTrace("AIFunction TrackTrace");

    return req.CreateResponse(HttpStatusCode.OK, "Hello!");
}
Run Code Online (Sandbox Code Playgroud)

输出:

[19.04.2018 06:41:06] Executing HTTP request: {
[19.04.2018 06:41:06]   "requestId": "d193e55d-305f-4a0c-9f17-40c0a062500a",
[19.04.2018 06:41:06]   "method": "GET",
[19.04.2018 06:41:06]   "uri": "/api/Function1"
[19.04.2018 06:41:06] }
[19.04.2018 06:41:06] Function started (Id=568798ce-f15f-4d8c-ac2b-c92aa47047a5)
[19.04.2018 06:41:06] Executing 'Function1' (Reason='This function was programmatically called via the host APIs.', Id=568798ce-f15f-4d8c-ac2b-c92aa47047a5)
[19.04.2018 06:41:06] C# ServiceBus queue trigger function processed message:
[19.04.2018 06:41:06] C# ServiceBus queue trigger function to test Application Insight Logging
[19.04.2018 06:41:07] Function completed (Success, Id=568798ce-f15f-4d8c-ac2b-c92aa47047a5, Duration=197ms)
[19.04.2018 06:41:07] Executed 'Function1' (Succeeded, Id=568798ce-f15f-4d8c-ac2b-c92aa47047a5)
[19.04.2018 06:41:07] Executed HTTP request: {
[19.04.2018 06:41:07]   "requestId": "d193e55d-305f-4a0c-9f17-40c0a062500a",
[19.04.2018 06:41:07]   "method": "GET",
[19.04.2018 06:41:07]   "uri": "/api/Function1",
[19.04.2018 06:41:07]   "authorizationLevel": "Anonymous",
[19.04.2018 06:41:07]   "status": "OK"
[19.04.2018 06:41:07] }
Run Code Online (Sandbox Code Playgroud)

如果只想本地登录,可以将Info方法更改为例如Verbose

writer.Verbose("C# ServiceBus queue trigger function processed message: ");
writer.Verbose("C# ServiceBus queue trigger function to test Application Insight Logging");
Run Code Online (Sandbox Code Playgroud)

然后更新您的本地host.json文件:

{
  "tracing": {
    "consoleLevel": "verbose"
  }
}
Run Code Online (Sandbox Code Playgroud)

但是就我个人而言,一旦您实施了Application Insights,它就不会有用。