Skip to content

Commit ba2d4f1

Browse files
committed
Fixed multi logger issue
1 parent 838131c commit ba2d4f1

File tree

7 files changed

+133
-67
lines changed

7 files changed

+133
-67
lines changed

dotNet/CoreHelpers.Extensions.Logging.Tasks/TaskLoggerExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public static ILoggingBuilder AddTaskLogger(this ILoggingBuilder builder)
1616
=> logger.BeginScope<TaskLoggerState>(new TaskLoggerState() { TaskId = taskId, IsTaskAnnounced = true });
1717

1818
public static IDisposable? BeginNewTaskScope(this ILogger logger, string taskType, string taskSource, string taskWorker)
19-
=> logger.BeginScope<TaskLoggerState>(new TaskLoggerState() { TaskId = string.Empty, TaskType = taskType, TaskSource = taskSource, TaskWorker = taskSource, IsTaskAnnounced = false });
19+
=> logger.BeginScope<TaskLoggerState>(new TaskLoggerState() { TaskId = string.Empty, TaskType = taskType, TaskSource = taskSource, TaskWorker = taskWorker, IsTaskAnnounced = false });
2020
}
2121
}

dotNet/CoreHelpers.Extensions.Logging.Tasks/TaskLoggerProvider.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace CoreHelpers.Extensions.Logging.Tasks
77
internal class TaskLoggerProvider : ILoggerProvider
88
{
99
private ITaskLoggerFactory _taskLoggerFactory;
10+
private ILogger? _activeLogger = null;
1011

1112
public TaskLoggerProvider(ITaskLoggerFactory taskLoggerFactory)
1213
{
@@ -15,7 +16,10 @@ public TaskLoggerProvider(ITaskLoggerFactory taskLoggerFactory)
1516

1617
public ILogger CreateLogger(string categoryName)
1718
{
18-
return new TaskLogger(_taskLoggerFactory);
19+
if (_activeLogger == null)
20+
_activeLogger = new TaskLogger(_taskLoggerFactory);
21+
22+
return _activeLogger;
1923
}
2024

2125
public void Dispose()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
namespace CoreHelpers.TaskLogging.Sample
3+
{
4+
internal interface IProcessor
5+
{
6+
Task Execute();
7+
}
8+
}
9+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace CoreHelpers.TaskLogging.Sample
5+
{
6+
internal class ProcessorFailed : IProcessor
7+
{
8+
private ILogger<ProcessorFailed> _logger;
9+
10+
11+
public ProcessorFailed(ILogger<ProcessorFailed> logger)
12+
{
13+
_logger = logger;
14+
}
15+
16+
public async Task Execute()
17+
{
18+
await Task.CompletedTask;
19+
20+
try
21+
{
22+
// log some lines
23+
for (int i = 0; i < 10; i++)
24+
_logger.LogInformation($"{i} Hello World Task 2");
25+
26+
throw new Exception("I failed!");
27+
}
28+
catch (Exception e)
29+
{
30+
_logger.LogError(e, "Unknown Error");
31+
}
32+
}
33+
}
34+
}
35+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace CoreHelpers.TaskLogging.Sample
5+
{
6+
internal class ProcessorSuccess : IProcessor
7+
{
8+
private ILogger<ProcessorSuccess> _logger;
9+
10+
11+
public ProcessorSuccess(ILogger<ProcessorSuccess> logger)
12+
{
13+
_logger = logger;
14+
}
15+
16+
public async Task Execute()
17+
{
18+
_logger.LogInformation("Hello from the processor");
19+
await Task.CompletedTask;
20+
}
21+
}
22+
}
23+

dotNet/CoreHelpers.TaskLogging.Sample/Program.cs

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Hosting;
55
using Microsoft.Extensions.Logging;
66
using CoreHelpers.Extensions.Logging.Tasks;
7+
using CoreHelpers.TaskLogging.Sample;
78

89
// configure the services that they us the emulator
910
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
@@ -17,73 +18,17 @@
1718
.AddConsole()
1819
.AddTaskLogger());
1920

21+
builder.Services.AddTransient<IProcessor, ProcessorSuccess>();
22+
builder.Services.AddTransient<IProcessor, ProcessorFailed>();
23+
builder.Services.AddTransient<Worker>();
24+
2025
// build the host
2126
using IHost host = builder.Build();
2227

23-
// get the task logger factory
24-
var taskLoggerFactory = host.Services.GetService<ITaskLoggerFactory>();
25-
if (taskLoggerFactory == null)
26-
throw new NullReferenceException();
27-
28-
29-
// define a bit meta data
30-
var metaData = new Dictionary<string, string>()
31-
{
32-
{ "app", "CoreHelpers.TaskLogging.Sample"},
33-
{ "class", "Main"}
34-
};
35-
36-
// Announce a new task in the system
37-
var taskId = await taskLoggerFactory.AnnounceTask("SyncMyMails", "q:Queue01", "Container01", metaData);
38-
39-
// ... this is the place where the application can do other topics, be aware
40-
// the task is still announced and exists as pending task ...
41-
42-
// get a logger factory
43-
var loggerFactory = host.Services.GetService<ILoggerFactory>();
44-
if (loggerFactory == null)
28+
// get the worker
29+
var worker = host.Services.GetService<Worker>();
30+
if (worker == null)
4531
throw new NullReferenceException();
4632

47-
// get a logger
48-
var logger = loggerFactory.CreateLogger("Main");
49-
50-
// generate scope
51-
using(logger.BeginScope("This is just a scope"))
52-
{
53-
using(logger.BeginScope("This is just another scope"))
54-
{
55-
// after this using all what is logged becomes part of the
56-
// task context
57-
using(logger.BeginTaskScope(taskId))
58-
{
59-
// log some lines
60-
for (int i = 0; i < 500; i++)
61-
logger.LogInformation($"{i} Hello World Task 1");
62-
}
63-
}
64-
65-
// announce the second task
66-
var taskId2 = await taskLoggerFactory.AnnounceTask("SomethingElseWithErrors", "q:Queue01", "Container01");
67-
68-
// this would be a second task
69-
using (logger.BeginTaskScope(taskId2))
70-
{
71-
try
72-
{
73-
// log some lines
74-
for (int i = 0; i < 10; i++)
75-
logger.LogInformation($"{i} Hello World Task 2");
76-
77-
throw new Exception("I failed!");
78-
} catch (Exception e)
79-
{
80-
logger.LogError(e, "Unknown Error");
81-
}
82-
}
83-
}
84-
85-
// generate a new task the system never announced before
86-
using(logger.BeginNewTaskScope("NewTask", "Q:Q2", "Cnt02"))
87-
{
88-
logger.LogInformation("hello new task");
89-
}
33+
// execute the work
34+
await worker.Process();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
using CoreHelpers.Extensions.Logging.Tasks;
4+
5+
namespace CoreHelpers.TaskLogging.Sample
6+
{
7+
internal class Worker
8+
{
9+
private ILogger<Worker> _logger;
10+
private IEnumerable<IProcessor> _processors;
11+
private ITaskLoggerFactory _taskLoggerFactory;
12+
13+
public Worker(ILogger<Worker> logger, IEnumerable<IProcessor> processors, ITaskLoggerFactory taskLoggerFactory)
14+
{
15+
_logger = logger;
16+
_processors = processors;
17+
_taskLoggerFactory = taskLoggerFactory;
18+
}
19+
20+
public async Task Process()
21+
{
22+
// execute the successprocessor
23+
using (_logger.BeginNewTaskScope("successjob", "q", "w"))
24+
{
25+
await _processors.Where(p => p is ProcessorSuccess).First().Execute();
26+
}
27+
28+
// execute the failedprocessor
29+
using (_logger.BeginNewTaskScope("failedjob", "q", "w"))
30+
{
31+
await _processors.Where(p => p is ProcessorFailed).First().Execute();
32+
}
33+
34+
// execute the succssprocesssor with announcement
35+
var metaData = new Dictionary<string, string>()
36+
{
37+
{ "app", "CoreHelpers.TaskLogging.Sample"},
38+
{ "class", "Main"}
39+
};
40+
41+
var taskId = await _taskLoggerFactory.AnnounceTask("announcejob", "q", "w", metaData);
42+
43+
using (_logger.BeginTaskScope(taskId))
44+
{
45+
await _processors.Where(p => p is ProcessorSuccess).First().Execute();
46+
}
47+
}
48+
}
49+
}
50+

0 commit comments

Comments
 (0)