[英]DI with ILogger in Azure webJob
我有一個在 .net framework 4.6 中創建的 azure webjob 項目。 我正在嘗試使用 ILogger 實現依賴注入並在應用程序洞察中記錄信息。
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();
}
}
應用設置.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 job模板,.net framework 4.6.1創建了一個webjob項目,步驟如下:
第 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
第 4 步:我的 program.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..........");
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.