当前位置: 首页 > 知识库问答 >
问题:

Azure函数自定义ILogger

毛成济
2023-03-14

希望一些比我更了解Azure功能的人能提供帮助。

开箱即用,您可以使用应用程序设置中的APPINSIGHTS_INSTRUMENTATIONKEY设置登录到应用程序Insights...这将在基本级别记录函数请求,然后允许您执行log.loginformation等操作。

这只是被跟踪器或ilogger所掩盖。

共有1个答案

梁昊天
2023-03-14

问题是,我不想将密钥存储在我的配置中,我想将它与应用程序的所有其他密钥一起存储在KeyVault中。

我们可以将APPINSIGHTS_INSTRUMENTATIONKEY存储在密钥库中。我推荐您可以使用Azure MSI函数。

我还为它做了一个演示。下面是我的详细步骤。

3.使用复制的INSTRUMENTATIONKEY vaule创建机密。

4.如果要从Azure应用程序中获取秘密,需要添加访问策略

5.用VS创建一个httptrigger函数,并添加下面的演示代码。

 public static async Task<TelemetryClient> GeTelemetryClient()
        {
            var azureServiceTokenProvider = new AzureServiceTokenProvider();

            var keyVaultClient =
                new KeyVaultClient(
                    new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));

            var secret = await keyVaultClient.GetSecretAsync("https://{keyvault}.vault.azure.net/secrets/{sceret}")
                .ConfigureAwait(false);
            return new TelemetryClient {InstrumentationKey = secret.Value};
        }

        // This correllates all telemetry with the current Function invocation
        private static void UpdateTelemetryContext(TelemetryContext context, ExecutionContext functionContext,
            string userName)
        {
            context.Operation.Id = functionContext.InvocationId.ToString();
            context.Operation.ParentId = functionContext.InvocationId.ToString();
            context.Operation.Name = functionContext.FunctionName;
            context.User.Id = userName;
        }

        [FunctionName("Function1")]

        public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req,
            ExecutionContext context, ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            DateTime start = DateTime.UtcNow;

            // 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;

            // Track an Event
            var telemetryClient = await GeTelemetryClient().ConfigureAwait(false);
            var evt = new EventTelemetry("Function called");
            UpdateTelemetryContext(evt.Context, context, name);

            telemetryClient.TrackEvent(evt);

            // Track a Metric
            var metric = new MetricTelemetry("Test Metric", DateTime.Now.Millisecond);
            UpdateTelemetryContext(metric.Context, context, name);
            telemetryClient.TrackMetric(metric);

            // Track a Dependency
            var dependency = new DependencyTelemetry
            {
                Name = "GET api/planets/1/",
                Target = "swapi.co",
                Data = "https://swapi.co/api/planets/1/",
                Timestamp = start,
                Duration = DateTime.UtcNow - start,
                Success = true
            };
            UpdateTelemetryContext(dependency.Context, context, name);
            telemetryClient.TrackDependency(dependency);

            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);
        }
    }
}
 类似资料:
  • 我做了这个自定义函数,并把它放在全局之外,这通常会工作。我也试着在主异步木偶函数中移动它,但也不起作用。这是一个简单的函数。在每个page evaluate函数中,我调用它并传递选择器。但是,它的说法没有定义和promise拒绝,这是奇怪的,因为函数不是promise....请帮助 我尝试将函数转换为异步函数,添加了一个新的参数页。然后,我将async添加到我的评估函数中,然后将puppeteer

  • XQuery提供编写自定义函数的功能。 下面列出了创建自定义函数的准则。 使用关键字来定义函数。 使用当前XML架构中定义的数据类型 将函数体包含在花括号内。 使用XML命名空间前缀函数的名称。 创建自定义函数时使用以下语法。 语法 示例 以下示例显示如何在XQuery中创建用户定义的函数。 XQuery表达式 输出结果 - 验证结果 要测试上述函数,用上面的XQuery表达式替换books.xq

  • 我在理解group_by如何在tidyverse中工作时遗漏了一些东西。示例将阐明: 我已经创建了下面的函数,它采用很少的参数,并计算tibble内部的最佳权重(可能不是最漂亮的,但似乎工作): 当我在tibble中只有一个组时,这个函数可以很好地工作。我创建函数的方法是尝试通过在一个函数上进行测试来实现它,希望在我稍后对数据进行切片时它会起作用。 然后,我希望可以使用mutate为我的多个组创建

  • 2. 自定义函数 我们不仅可以调用C标准库提供的函数,也可以定义自己的函数,事实上我们已经这么做了:我们定义了main函数。例如: int main(void) { int hour = 11; int minute = 59; printf("%d and %d hours\n", hour, minute / 60); return 0; } main函数的特殊之处在于执行程序时它自动

  • 我正在尝试为Azure Functions创建自己的自定义绑定。这项工作基于有关此功能的2篇wiki文章:https://github.com/Azure/azure-webjobs-sdk/wiki/Creating-custom-input-and-output-bindings和https://github.com/Azure/WebJobsExtensionSamples 对于示例项目,我

  • $this->db->call_function(); 这个方法用于执行一些 CodeIgniter 中没有定义的 PHP 数据库函数,而且 使用了一种平台独立的方式。举个例子,假设你要调用 mysql_get_client_info() 函数,这个函数 CodeIgniter 并不是原生支持的,你可以这样做: $this->db->call_function('get_client_info')