gpt4 book ai didi

c# - Azure webJob 中使用 ILogger 进行 DI

转载 作者:行者123 更新时间:2023-12-02 08:02:44 25 4
gpt4 key购买 nike

我有一个在 .net Framework 4.6 中创建的 azure webjob 项目。我正在尝试使用 ILogger 实现依赖注入(inject)并将信息记录在应用程序洞察中。

class Program
{
private static IConfigurationRoot configuration;

// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
public static void Main()
{
var config = new JobHostConfiguration();

if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();

IServiceCollection serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
//config.JobActivator = new CustomJobActivator(serviceCollection.BuildServiceProvider());
config.LoggerFactory = new LoggerFactory()
.AddApplicationInsights(configuration.GetSection("ApplicationInsights")["InstrumentationKey"], null);
config.UseTimers();
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}

private static void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddTransient<Functions, Functions>();
serviceCollection.AddLogging();
}
}

appSettings.json

{   
"ApplicationInsights": {
"InstrumentationKey": "8028437c-888-666-444-2cf3777106a8"
}
}

函数.cs

public class Functions
{
private readonly ILogger<Functions> _logger;

public Functions(ILogger<Functions> logger)
{
_logger = logger;
}

public void ProcessTimerMessage([TimerTrigger("0 */5 * * * *")]TimerInfo timerInfo, TextWriter log)
{
//LOG THIS IN APP INSIGHTS
_logger.LogError("Error");
}
}

我还尝试在ConfigureServices方法中添加以下代码。但还是没有运气。

var telemetryClient = new TelemetryClient(new TelemetryConfiguration()
{
InstrumentationKey = "<< Instrumentation Key >>"
});
serviceCollection.AddSingleton(x => telemetryClient).AddLogging();

只有跟踪日志会记录在应用程序洞察中,而记录器对象日志不会出现。请帮忙

最佳答案

我从 vs web 作业模板,.net Framework 4.6.1 创建了一个 webjob 项目,步骤如下:

第1步:创建项目 enter image description here

第 2 步:安装以下软件包:

Install-Package Microsoft.Azure.WebJobs -version 2.2.0
Install-Package Microsoft.Extensions.Logging -version 2.0.1
Install-Package Microsoft.Extensions.Logging.Console -version 2.0.1
Install-Package Microsoft.Azure.WebJobs.Logging.ApplicationInsights -version 2.2.0
Install-Package System.Configuration.ConfigurationManager -version 4.4.1

第3步:在app.config中,添加以下内容: enter image description here

第 4 步:我的程序.cs:

  using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.Configuration;

namespace WebJob7
{

class Program
{

static void Main()
{
var config = new JobHostConfiguration();
var instrumentationKey =
ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"];

config.DashboardConnectionString = "";

config.LoggerFactory = new LoggerFactory()
.AddApplicationInsights(instrumentationKey, null)
.AddConsole();

if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}

var host = new JobHost(config);

host.RunAndBlock();
}
}
}

第 5 步:Function.cs 中的我的代码:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace WebJob7
{
public class Functions
{
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger logger)
{
//you can directly use this line of code.
//logger.LogError(new Exception(),"it is a test error...");

//or use the following code
try
{
int i = int.Parse("123er");
}
catch(Exception ex)
{
logger.LogError(ex,"it's a exception here 0927..........");
}

}
}
}

执行后,日志显示在azure门户中 -> go as Exception: enter image description here

点击查看详情: enter image description here

关于c# - Azure webJob 中使用 ILogger 进行 DI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52421551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com