據我所知,目前我們無法直接在您的本地天藍色功能項目中包含Application Insights。
這是一種解決方法:
您需要自行實施。從Nuget包管理器安裝Microsoft.ApplicationInsights之後。
然後使用TelemetryClient將日誌發送到天藍色。
更多細節,你可以參考下面的代碼:
[FunctionName("HttpTriggerCSharp")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
var appInsights = GetTelemetryClient();
//track an event
appInsights.TrackEvent("I am starting now. I'm timer triggered");
// track a numeric value
appInsights.TrackMetric("Ticks based on current time", DateTime.Now.Ticks);
// track an exception
appInsights.TrackException(new Exception($"Random exception at {DateTime.Now}"));
// send data to azure
appInsights.Flush();
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
name = name ?? data?.name;
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
private static TelemetryClient GetTelemetryClient()
{
var telemetryClient = new TelemetryClient();
telemetryClient.InstrumentationKey = "Your InstrumentationKey";
return telemetryClient;
}
}
}
結果:
據我所知,如果你想記錄信息到Application Insights。我建議你可以考慮使用Microsoft.ApplicationInsights包並使用InstrumentationKey創建TelemetryClient。更多細節,你可以參考這篇文章(https://cmatskas.com/azure-functions-custom-logging-with-appinsights/)。 –
引用的文章說本身已經過時,因爲Azure函數已經與Application Insights集成。但是,該集成在Visual Studio中無法使用,以查看VS中的日誌而不是門戶。 – gabomgp