如何通过 Azure Functions 在本地使用 Application Insights?
How can I use Application Insights locally with Azure Functions?
我正在使用 ILogger 接口在我的 Azure 函数中记录事件。我可以在 Azure 中发布它并将其连接到 Azure 中的 Application Insights。
我想在开发期间在 Visual Studio 的 Application Insights 中查看我的日志。在 here 中,我可以看到在 ASP.NET 核心 Web 应用程序中将一些代码放入 Startup.cs 中是可能的。使用 VS 2017 中的工具的新项目模板,Azure Functions 是否可以实现类似的功能?
我正在使用 VS 2017 和 Azure Function CLI 1.0.0-beta-100。
据我所知,目前我们无法直接将 Application Insights 包含在您本地的 azure Functions 项目中。
解决方法如下:
需要自己实现。从 Nuget 包管理器安装 Microsoft.ApplicationInsights 后。
然后使用TelemetryClient将日志发送到Azure。
更多详情,您可以参考以下代码:
[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 (AI) 遥测添加到 Azure Functions 简单明了。
您只需要官方文档中的这些two步骤;
- 创建一个 Application Insights 实例。
- 应用程序类型应设置为常规
- 获取检测密钥
- 更新您的函数应用程序的设置
- 添加应用程序设置 –
APPINSIGHTS_INSTRUMENTATIONKEY = {Instrumentation Key}
然而,不(那么)明显的是如何在本地开发功能应用程序时捕获 AI 遥测数据,然后才让它进入云端。首先,您需要 prepare Azure Functions 的本地开发环境。那么你就差不多准备好了。您只需要在 local.settings.json
文件上重复上面的第二步。所以你的文件应该看起来像这样;
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_EXTENSION_VERSION": "beta",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"HOST_NAME": "localhost:7072",
"APPINSIGHTS_INSTRUMENTATIONKEY": "11111111-2222-3333-4444-555555555555"
},
"Host": {
"LocalHttpPort": 7072
}
}
在我的例子中,我有 2 个 AI 实例,我的 Azure 门户和我的本地开发环境 (local.settings.json
) 中的 APPINSIGHTS_INSTRUMENTATIONKEY
值不同,这样我就不会混淆生产和开发遥测.
因此,显然由于对库所做的更改,有时它会停止工作。所以这就是帮助我在 2019 年 4 月让它发挥作用的原因。希望它能帮助其他人。
我没有添加任何日志或任何特定于我的函数应用程序的内容,现在对我来说,它几乎是将所有控制台数据打印到我的应用程序见解中的跟踪日志中(在本地,不需要在蔚蓝)。另外,我的测试场景是 TargetFramework netstandard2.0
和 AzureFunctionsVersion 2
.
上的 运行
首先您需要将这个 NuGet 包添加到函数应用程序中:https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/
然后您必须将一个空文件添加到您的项目根目录:ApplicationInsights.config
您不需要在构建或任何其他内容时将其复制到输出,这只是必须存在的东西或 Application Insights不会在本地工作。
然后您需要在 local.settings.json
中为您的 APPINSIGHTS_INSTRUMENTATIONKEY
添加一些值。这是它与常规控制台应用程序不同的地方,在常规控制台应用程序中,即使检测密钥为空,它也会在本地运行。在功能应用程序中,显然您需要在那里添加一些值。它可以是任何东西,我已经做到了并且效果很好:
"APPINSIGHTS_INSTRUMENTATIONKEY": "you-need-to-have-anything-at-all-here-so-it-will-work-locally"
我正在使用 ILogger 接口在我的 Azure 函数中记录事件。我可以在 Azure 中发布它并将其连接到 Azure 中的 Application Insights。
我想在开发期间在 Visual Studio 的 Application Insights 中查看我的日志。在 here 中,我可以看到在 ASP.NET 核心 Web 应用程序中将一些代码放入 Startup.cs 中是可能的。使用 VS 2017 中的工具的新项目模板,Azure Functions 是否可以实现类似的功能?
我正在使用 VS 2017 和 Azure Function CLI 1.0.0-beta-100。
据我所知,目前我们无法直接将 Application Insights 包含在您本地的 azure Functions 项目中。
解决方法如下:
需要自己实现。从 Nuget 包管理器安装 Microsoft.ApplicationInsights 后。
然后使用TelemetryClient将日志发送到Azure。
更多详情,您可以参考以下代码:
[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 (AI) 遥测添加到 Azure Functions 简单明了。
您只需要官方文档中的这些two步骤;
- 创建一个 Application Insights 实例。
- 应用程序类型应设置为常规
- 获取检测密钥
- 更新您的函数应用程序的设置
- 添加应用程序设置 –
APPINSIGHTS_INSTRUMENTATIONKEY = {Instrumentation Key}
- 添加应用程序设置 –
然而,不(那么)明显的是如何在本地开发功能应用程序时捕获 AI 遥测数据,然后才让它进入云端。首先,您需要 prepare Azure Functions 的本地开发环境。那么你就差不多准备好了。您只需要在 local.settings.json
文件上重复上面的第二步。所以你的文件应该看起来像这样;
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_EXTENSION_VERSION": "beta",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"HOST_NAME": "localhost:7072",
"APPINSIGHTS_INSTRUMENTATIONKEY": "11111111-2222-3333-4444-555555555555"
},
"Host": {
"LocalHttpPort": 7072
}
}
在我的例子中,我有 2 个 AI 实例,我的 Azure 门户和我的本地开发环境 (local.settings.json
) 中的 APPINSIGHTS_INSTRUMENTATIONKEY
值不同,这样我就不会混淆生产和开发遥测.
因此,显然由于对库所做的更改,有时它会停止工作。所以这就是帮助我在 2019 年 4 月让它发挥作用的原因。希望它能帮助其他人。
我没有添加任何日志或任何特定于我的函数应用程序的内容,现在对我来说,它几乎是将所有控制台数据打印到我的应用程序见解中的跟踪日志中(在本地,不需要在蔚蓝)。另外,我的测试场景是 TargetFramework netstandard2.0
和 AzureFunctionsVersion 2
.
首先您需要将这个 NuGet 包添加到函数应用程序中:https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/
然后您必须将一个空文件添加到您的项目根目录:ApplicationInsights.config
您不需要在构建或任何其他内容时将其复制到输出,这只是必须存在的东西或 Application Insights不会在本地工作。
然后您需要在 local.settings.json
中为您的 APPINSIGHTS_INSTRUMENTATIONKEY
添加一些值。这是它与常规控制台应用程序不同的地方,在常规控制台应用程序中,即使检测密钥为空,它也会在本地运行。在功能应用程序中,显然您需要在那里添加一些值。它可以是任何东西,我已经做到了并且效果很好:
"APPINSIGHTS_INSTRUMENTATIONKEY": "you-need-to-have-anything-at-all-here-so-it-will-work-locally"