Skip to content

Commit 546874b

Browse files
committed
Did I not save the code?
1 parent fe972d9 commit 546874b

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
internal sealed class JobRunner(ILogger<JobRunner> logger)
4+
{
5+
public async Task RunAsync()
6+
{
7+
logger.LogInformation("Starting job...");
8+
9+
// Simulate work
10+
await Task.Delay(1000);
11+
12+
// Simulate failure
13+
// throw new InvalidOperationException("Something went wrong!");
14+
15+
logger.LogInformation("Job completed successfully.");
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
5+
var builder = Host.CreateApplicationBuilder(args);
6+
builder.Services.AddSingleton<JobRunner>();
7+
8+
using var host = builder.Build();
9+
10+
try
11+
{
12+
var runner = host.Services.GetRequiredService<JobRunner>();
13+
14+
await runner.RunAsync();
15+
16+
return 0; // success
17+
}
18+
catch (Exception ex)
19+
{
20+
var logger = host.Services.GetRequiredService<ILogger<Program>>();
21+
22+
logger.LogError(ex, "Unhandled exception occurred during job execution.");
23+
24+
return 1; // failure
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>true</ImplicitUsings>
7+
<RootNamespace>ShortLived.App</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.4" />
12+
</ItemGroup>
13+
</Project>

0 commit comments

Comments
 (0)