首页 > 解决方案 > how to use both the log writer and app insights within azure function?

问题描述

I'd like to send trace and other events to both the ILogger as well as application insights.

I know that I can simply do this:

    [FunctionName ("OnSomethingHttpTriggered")]
    public static async System.Threading.Tasks.Task RunAsync ([QueueTrigger ("myq", Connection = "StorageAccountConnection")] string payload, ILogger log) {
        var telemetry = new TelemetryClient {
        InstrumentationKey = Environment.GetEnvironmentVariable ("APPINSIGHTS_INSTRUMENTATIONKEY")
        };
        log.LogInformation ($"C# Queue trigger function processed: {payload}");
        telemetry.TrackEvent ($"C# Queue trigger function processed: {payload}");

        var isPayloadValidSchema = SchemaValidator.IsValid (payload);
        if (!isPayloadValidSchema) {
            log.LogError ($"This visit is not valid {payload}");
            telemetry.TrackEvent ($"This visit is not valid {payload}");
            return;
        }
    }

But as you can see I would need to double my code each time for ILogger and TelemetryClient.

How do I avoid this repetition?

标签: c#.netazureazure-functionsazure-application-insights

解决方案


我们不需要telemtryclient发送 的输出,一旦我们在应用程序设置中设置(本地在 local.setting.json 的值中)ILogger,这些将默认显示为 Application Insights中的跟踪。APPINSIGHTS_INSTRUMENTATIONKEY我们telemtryclient用来发送自定义遥测数据


推荐阅读