Skip to content

Commit c0d1a96

Browse files
committed
Custom Factory Logging sample for NServiceBus 10 #7319
1 parent a95c410 commit c0d1a96

File tree

9 files changed

+269
-0
lines changed

9 files changed

+269
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.8.34408.163
4+
MinimumVisualStudioVersion = 15.0.26730.12
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "Sample\Sample.csproj", "{48F718EE-6C45-41BA-80EC-81BF34D4A623}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
EndGlobalSection
15+
GlobalSection(SolutionProperties) = preSolution
16+
HideSolutionNode = FALSE
17+
EndGlobalSection
18+
EndGlobal
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System;
2+
using NServiceBus.Logging;
3+
4+
#region log
5+
class ConsoleLog(string name, LogLevel level) :
6+
ILog
7+
{
8+
public bool IsDebugEnabled { get; } = LogLevel.Debug >= level;
9+
public bool IsInfoEnabled { get; } = LogLevel.Info >= level;
10+
public bool IsWarnEnabled { get; } = LogLevel.Warn >= level;
11+
public bool IsErrorEnabled { get; } = LogLevel.Error >= level;
12+
public bool IsFatalEnabled { get; } = LogLevel.Fatal >= level;
13+
14+
void Write(string level, string message, Exception exception)
15+
{
16+
Console.WriteLine($"{name}. {level}. {message}. Exception: {exception}");
17+
}
18+
19+
void Write(string level, string message)
20+
{
21+
Console.WriteLine($"{name}. {level}. {message}.");
22+
}
23+
24+
void Write(string level, string format, params object[] args)
25+
{
26+
format = $"{name}. {level}. {format}";
27+
Console.WriteLine(format, args);
28+
}
29+
30+
public void Debug(string message)
31+
{
32+
if (IsDebugEnabled)
33+
{
34+
Write("Debug", message);
35+
}
36+
}
37+
38+
public void Debug(string message, Exception exception)
39+
{
40+
if (IsDebugEnabled)
41+
{
42+
Write("Debug", message, exception);
43+
}
44+
}
45+
46+
public void DebugFormat(string format, params object[] args)
47+
{
48+
if (IsDebugEnabled)
49+
{
50+
Write("Debug", format, args);
51+
}
52+
}
53+
// Other log methods excluded for brevity
54+
#endregion
55+
public void Info(string message)
56+
{
57+
if (IsInfoEnabled)
58+
{
59+
Write("Info", message);
60+
}
61+
}
62+
63+
public void Info(string message, Exception exception)
64+
{
65+
if (IsInfoEnabled)
66+
{
67+
Write("Info", message, exception);
68+
}
69+
}
70+
71+
public void InfoFormat(string format, params object[] args)
72+
{
73+
if (IsInfoEnabled)
74+
{
75+
Write("Info", format, args);
76+
}
77+
}
78+
79+
public void Warn(string message)
80+
{
81+
if (IsWarnEnabled)
82+
{
83+
Write("Warn", message);
84+
}
85+
}
86+
87+
public void Warn(string message, Exception exception)
88+
{
89+
if (IsWarnEnabled)
90+
{
91+
Write("Warn", message, exception);
92+
}
93+
}
94+
95+
public void WarnFormat(string format, params object[] args)
96+
{
97+
if (IsWarnEnabled)
98+
{
99+
Write("Warn", format, args);
100+
}
101+
}
102+
103+
public void Error(string message)
104+
{
105+
if (IsErrorEnabled)
106+
{
107+
Write("Error", message);
108+
}
109+
}
110+
111+
public void Error(string message, Exception exception)
112+
{
113+
if (IsErrorEnabled)
114+
{
115+
Write("Error", message, exception);
116+
}
117+
}
118+
119+
public void ErrorFormat(string format, params object[] args)
120+
{
121+
if (IsErrorEnabled)
122+
{
123+
Write("Error", format, args);
124+
}
125+
}
126+
127+
public void Fatal(string message)
128+
{
129+
if (IsFatalEnabled)
130+
{
131+
Write("Fatal", message);
132+
}
133+
}
134+
135+
public void Fatal(string message, Exception exception)
136+
{
137+
if (IsFatalEnabled)
138+
{
139+
Write("Fatal", message, exception);
140+
}
141+
}
142+
143+
public void FatalFormat(string format, params object[] args)
144+
{
145+
if (IsFatalEnabled)
146+
{
147+
Write("Fatal", format, args);
148+
}
149+
}
150+
151+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#region definition
2+
using NServiceBus.Logging;
3+
4+
class ConsoleLoggerDefinition :
5+
LoggingFactoryDefinition
6+
{
7+
LogLevel level = LogLevel.Info;
8+
9+
public void Level(LogLevel level)
10+
{
11+
this.level = level;
12+
}
13+
14+
protected override ILoggerFactory GetLoggingFactory()
15+
{
16+
return new ConsoleLoggerFactory(level);
17+
}
18+
}
19+
#endregion
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using NServiceBus.Logging;
3+
#region factory
4+
5+
class ConsoleLoggerFactory(LogLevel level) :
6+
ILoggerFactory
7+
{
8+
public ILog GetLogger(Type type)
9+
{
10+
return GetLogger(type.FullName);
11+
}
12+
13+
public ILog GetLogger(string name)
14+
{
15+
return new ConsoleLog(name, level);
16+
}
17+
}
18+
19+
#endregion
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
using NServiceBus.Logging;
4+
5+
public class MyHandler :
6+
IHandleMessages<MyMessage>
7+
{
8+
static ILog log = LogManager.GetLogger<MyHandler>();
9+
10+
public Task Handle(MyMessage message, IMessageHandlerContext context)
11+
{
12+
log.Info("Hello from MyHandler");
13+
return Task.CompletedTask;
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using NServiceBus;
2+
3+
public class MyMessage :
4+
IMessage
5+
{
6+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using NServiceBus;
5+
using NServiceBus.Logging;
6+
7+
var host = Host.CreateDefaultBuilder(args)
8+
.UseConsoleLifetime()
9+
.UseNServiceBus(_ =>
10+
{
11+
var loggerDefinition = LogManager.Use<ConsoleLoggerDefinition>();
12+
loggerDefinition.Level(LogLevel.Info);
13+
14+
var endpointConfiguration = new EndpointConfiguration("Samples.Logging.CustomFactory");
15+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
16+
endpointConfiguration.UseTransport<LearningTransport>();
17+
18+
return endpointConfiguration;
19+
})
20+
.Build();
21+
22+
await host.StartAsync();
23+
24+
var endpointInstance = host.Services.GetRequiredService<IMessageSession>();
25+
await endpointInstance.SendLocal(new MyMessage());
26+
27+
Console.WriteLine("Press any key to exit");
28+
Console.ReadKey();
29+
30+
await host.StopAsync();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
9+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
10+
</ItemGroup>
11+
</Project>

samples/logging/custom-factory/Core_10/prerelease.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)