我想向ILogger
以及应用程序见解发送跟踪和其他事件。
我知道我可以简单地做到这一点:
[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;
}
}
但是您可以看到,ILogger
和TelemetryClient
每次我都需要加倍代码。
如何避免这种重复?
答案 0 :(得分:1)
我们不需要telemtryclient
来发送ILogger
的输出,默认情况下,在我们在应用程序设置中设置APPINSIGHTS_INSTRUMENTATIONKEY
时,它们将在appear as traces of local.setting.json)。我们使用telemtryclient
发送custom telemetry data。