程序.cs
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
IConfiguration configuration = hostContext.Configuration;
AppSettings.Init(configuration.GetSection("AppSettings"));
ConfigureLogger().Wait();
services.AddHostedService<Worker>();
})
.UseWindowsService()
.Build();
host.Run();
async Task ConfigureLogger()
{
// Set up Application Insights TelemetryConfiguration
string appInsightsCS = await ApiHelper.GetAppInsightsConnectionString();
if (!String.IsNullOrEmpty(appInsightsCS))
{
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.CreateDefault();
telemetryConfiguration.ConnectionString = appInsightsCS;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.Enrich.FromLogContext()
.Enrich.WithProperty("ApplicationName", "Extractor")
.Enrich.WithProperty("Practice Id", AppSettings.Instance.PracticeId)
.Enrich.WithProperty("Practice Name", AppSettings.Instance.PracticeName)
.Enrich.WithProperty("PmsType", AppSettings.Instance.PmsType)
.WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Traces)
.CreateLogger();
}
else
{
Console.WriteLine("Appplication Inssights connection not found");
}
}
Worker.cs
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await new MyScheduler().Initiate();
}
}
我的调度程序
public class MyScheduler
{
public async Task Initiate()
{
try
{
ScheduleInformation scheduleInfo = await ApiHelper.LoadScheduleInfo();
if(scheduleInfo != null)
{
SharedDataService.PracticeConfigs = scheduleInfo.PracticeConfigs;
await ScheduleJob(scheduleInfo);
}
}
}
}
public async Task ScheduleJob(ScheduleInformation scheduleInfo)
{
// Create a scheduler
IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();
// Define a job
IJobDetail job = JobBuilder.Create<ExtractionJob>()
.WithIdentity($"Schedule-{scheduleInfo.Id}", $"JobGroup{scheduleInfo.Id}")
.Build();
//set job parameters
job.JobDataMap.Put("JobParams", scheduleInfo);
// Define a trigger with the retrieved cron expression
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity($"ScheduleTrigger-{scheduleInfo.Id}", $"TriggerGroup{scheduleInfo.Id}")
.StartNow()
.WithCronSchedule(scheduleInfo.CronExpression)
.Build();
Log.Information($"Schedule started. Id = {scheduleInfo.Id}");
// Schedule the job with the trigger
await scheduler.ScheduleJob(job, trigger);
await scheduler.Start();
}
提取工作
当我注入 ILogger 时,然后在它停止工作后。
public class ExtractionJob : IJob
{
readonly AzureHelper _azureHelper;
readonly ILogger<ExtractionJob> _logger;
public ExtractionJob(ILogger<ExtractionJob> logger)
{
_azureHelper = new AzureHelper();
_logger = logger;
}
public async Task Execute(IJobExecutionContext context)
{ }
}
我认为你应该在某个地方观察到一个异常,指出
ExtractionJob
的构造函数无法解析,我认为这是由于需要 DI 调用。
尝试将以下内容添加到您的应用程序启动中:
builder.Host.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
//add your provider of choice
});
请在此处阅读更多信息:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-8.0