C# C中Azure函数的Application Insight日志记录

C# C中Azure函数的Application Insight日志记录,c#,azure,azure-functions,azure-application-insights,azure-functions-runtime,C#,Azure,Azure Functions,Azure Application Insights,Azure Functions Runtime,我们在C中有服务总线触发的Azure函数。我想在Application Insight中记录诸如调用的函数、结果、异常等信息。我已将TraceWriter转换为Ilogger以获取日志信息。我想要实现的是在本地和ApplicationInsight实例上的控制台上进行日志记录。实现这一目标的最佳方式是什么 public static class AIFunction { private static string key = TelemetryConfiguration.Acti

我们在C中有服务总线触发的Azure函数。我想在Application Insight中记录诸如调用的函数、结果、异常等信息。我已将TraceWriter转换为Ilogger以获取日志信息。我想要实现的是在本地和ApplicationInsight实例上的控制台上进行日志记录。实现这一目标的最佳方式是什么

    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");
    }
}
您只需另外注入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!");
}
如果您只想在本地登录,您可以将Info方法更改为Verbose:

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

但就我个人而言,一旦您实现了应用程序洞察,我觉得它就没有什么用处了。

您可以另外注入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!");
}
如果您只想在本地登录,您可以将Info方法更改为Verbose:

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


但就我个人而言,一旦您实现了应用程序洞察,我就觉得它没有什么用处。

您是否更新了针对VS的Azure功能扩展?我想在最新更新后,我会从控制台中的ILogger获取日志。TraceWriter和ILogger之间有什么区别?您是否为VS更新了Azure函数扩展?我想在最新更新后,我会从控制台中的ILogger获取日志。TraceWriter和ILogger之间有什么区别?对于本地调试,它将非常有用。。或者我错过了一些东西,你能详细说明一下吗?@Kcsss很酷,如果你发现任何问题,请告诉我,如果它解决了你的问题,请记住接受这个答案:当然。几件事:1]一旦我们使用InstrumentationKey配置函数应用程序,然后当我们在本地运行函数时,这些日志也会记录到Application insight?或者只有那些为部署的功能而记录的日志?@Kcsss一旦引入InstrumentationKey日志就会发送到云中的AI实例,因此是的,在本地使用ILogger将在Application Insights中记录数据。对于本地调试,它将非常有用。。或者我错过了一些东西,你能详细说明一下吗?@Kcsss很酷,如果你发现任何问题,请告诉我,如果它解决了你的问题,请记住接受这个答案:当然。几件事:1]一旦我们使用InstrumentationKey配置函数应用程序,然后当我们在本地运行函数时,这些日志也会记录到Application insight?或者只有那些为部署的功能记录的日志?@Kcsss一旦引入InstrumentationKey日志,就会发送到云中的AI实例,因此是的,在本地使用ILogger将在Application Insights中记录数据。
writer.Verbose("C# ServiceBus queue trigger function processed message: ");
writer.Verbose("C# ServiceBus queue trigger function to test Application Insight Logging");
{
  "tracing": {
    "consoleLevel": "verbose"
  }
}