Skip to content

Commit c489a31

Browse files
authored
Merge pull request connamara#971 from gbirchmeier/ilogger-rebased-squash
Move to Microsoft.Extensions.Logging (squashed/rebased connamara#899)
2 parents 9669a1e + ad612c7 commit c489a31

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+901
-622
lines changed

AcceptanceTest/ATApplication.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Text.RegularExpressions;
3+
using Microsoft.Extensions.Logging;
44
using QuickFix;
55
using QuickFix.Fields;
66

@@ -195,7 +195,8 @@ public void FromApp(Message message, SessionID sessionId)
195195
}
196196
catch (System.Exception e)
197197
{
198-
Session.LookupSession(sessionId)?.Log.OnEvent($"Exception during FromApp: {e}\n while processing msg ({message})");
198+
Session.LookupSession(sessionId)?.Log.Log(LogLevel.Error, e,
199+
"Exception during FromApp: {Error}\n while processing msg ({Message})", e, message);
199200
}
200201
}
201202

AcceptanceTest/AcceptanceTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<PrivateAssets>all</PrivateAssets>
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2121
</PackageReference>
22+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
2223
</ItemGroup>
2324

2425
<ItemGroup>

Examples/Executor/Examples.Executor.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@
4040
</None>
4141
</ItemGroup>
4242

43+
<ItemGroup>
44+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
45+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
46+
</ItemGroup>
47+
4348
</Project>

Examples/Executor/Program.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Microsoft.Extensions.Logging;
23
using QuickFix;
34
using QuickFix.Logger;
45
using QuickFix.Store;
@@ -27,9 +28,22 @@ static void Main(string[] args)
2728
SessionSettings settings = new SessionSettings(args[0]);
2829
IApplication executorApp = new Executor();
2930
IMessageStoreFactory storeFactory = new FileStoreFactory(settings);
31+
32+
/*
33+
// legacy logging interface
3034
ILogFactory logFactory = new ScreenLogFactory(settings);
31-
//ILogFactory logFactory = new FileLogFactory(settings);
3235
ThreadedSocketAcceptor acceptor = new ThreadedSocketAcceptor(executorApp, storeFactory, settings, logFactory);
36+
*/
37+
38+
// v1.14: you can use Microsoft.Extensions.Logging instead
39+
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
40+
{
41+
builder.SetMinimumLevel(LogLevel.Trace);
42+
builder.AddConsole();
43+
});
44+
ThreadedSocketAcceptor acceptor =
45+
new ThreadedSocketAcceptor(executorApp, storeFactory, settings, loggerFactory);
46+
3347
HttpServer srv = new HttpServer(HttpServerPrefix, settings);
3448

3549
acceptor.Start();

Examples/SimpleAcceptor/Examples.SimpleAcceptor.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@
3434
</None>
3535
</ItemGroup>
3636

37+
<ItemGroup>
38+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
39+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
40+
</ItemGroup>
41+
3742
</Project>

Examples/SimpleAcceptor/Program.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Microsoft.Extensions.Logging;
23
using QuickFix;
34
using QuickFix.Logger;
45
using QuickFix.Store;
@@ -30,8 +31,18 @@ static void Main(string[] args)
3031
SessionSettings settings = new SessionSettings(args[0]);
3132
IApplication app = new SimpleAcceptorApp();
3233
IMessageStoreFactory storeFactory = new FileStoreFactory(settings);
33-
ILogFactory logFactory = new FileLogFactory(settings);
34-
IAcceptor acceptor = new ThreadedSocketAcceptor(app, storeFactory, settings, logFactory);
34+
35+
ILogFactory logFactory = new ScreenLogFactory(settings);
36+
ThreadedSocketAcceptor acceptor = new ThreadedSocketAcceptor(app, storeFactory, settings, logFactory);
37+
38+
/*
39+
// v1.14: you can use Microsoft.Extensions.Logging instead
40+
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
41+
{
42+
builder.AddConsole();
43+
});
44+
IAcceptor acceptor = new ThreadedSocketAcceptor(app, storeFactory, settings, loggerFactory);
45+
*/
3546

3647
acceptor.Start();
3748
Console.WriteLine("press <enter> to quit");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
log/
2+
store/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace SerilogLog;
2+
3+
public class AppSettings
4+
{
5+
public required string ConfigFilePath { get; set; }
6+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.Options;
6+
using QuickFix;
7+
using QuickFix.Store;
8+
using QuickFix.Transport;
9+
10+
namespace SerilogLog;
11+
12+
public class FixBackgroundService(IOptions<AppSettings> appSettings, ILoggerFactory loggerFactory) : IHostedService, IApplication
13+
{
14+
private SocketInitiator? _initiator;
15+
16+
public Task StartAsync(CancellationToken cancellationToken)
17+
{
18+
var settings = new SessionSettings(appSettings.Value.ConfigFilePath);
19+
20+
var storeFactory = new FileStoreFactory(settings);
21+
_initiator = new SocketInitiator(
22+
this,
23+
storeFactory,
24+
settings,
25+
loggerFactory);
26+
27+
_initiator.Start();
28+
return Task.CompletedTask;
29+
}
30+
31+
public Task StopAsync(CancellationToken cancellationToken)
32+
{
33+
_initiator?.Stop();
34+
return Task.CompletedTask;
35+
}
36+
37+
public void ToAdmin(Message message, SessionID sessionId)
38+
{
39+
}
40+
41+
public void FromAdmin(Message message, SessionID sessionId)
42+
{
43+
}
44+
45+
public void ToApp(Message message, SessionID sessionId)
46+
{
47+
}
48+
49+
public void FromApp(Message message, SessionID sessionId)
50+
{
51+
}
52+
53+
public void OnCreate(SessionID sessionId)
54+
{
55+
}
56+
57+
public void OnLogout(SessionID sessionId)
58+
{
59+
}
60+
61+
public void OnLogon(SessionID sessionId)
62+
{
63+
}
64+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using Serilog;
6+
using Serilog.Core;
7+
using Serilog.Filters;
8+
9+
namespace SerilogLog;
10+
11+
class Program
12+
{
13+
static async Task Main(string[] args)
14+
{
15+
Log.Logger = new LoggerConfiguration()
16+
.Enrich.FromLogContext()
17+
.MinimumLevel.Debug()
18+
.WriteTo.Console()
19+
.WriteTo.Logger(l => l
20+
.Filter
21+
.ByIncludingOnly(Matching.FromSource("QuickFix.SessionLogs"))
22+
.Filter
23+
.ByIncludingOnly("EventId.Id in [7702, 7703]") // QuickFix.Logger.LogEventIds.IncomingMessage.Id
24+
// and .OutgoingMessage.Id
25+
.WriteTo.Map(Constants.SourceContextPropertyName, "", (source, lc) =>
26+
{
27+
var fileName = GetSessionIdFromSourceContext(source);
28+
lc.File($"log/{fileName}.messages.log", rollingInterval: RollingInterval.Hour, outputTemplate:"{Message:lj}{NewLine}");
29+
})
30+
)
31+
.WriteTo.Logger(l => l
32+
.Filter
33+
.ByIncludingOnly(Matching.FromSource("QuickFix.SessionLogs"))
34+
.Filter
35+
.ByExcluding("EventId.Id in [7702, 7703]")
36+
.WriteTo.Map(Constants.SourceContextPropertyName, "", (source, lc) =>
37+
{
38+
var fileName = GetSessionIdFromSourceContext(source);
39+
lc.File($"log/{fileName}.event.log", rollingInterval: RollingInterval.Hour, outputTemplate:"{Message:lj}{NewLine}");
40+
})
41+
)
42+
.WriteTo.Logger(l => l
43+
.Filter
44+
.ByIncludingOnly(Matching.FromSource("QuickFix"))
45+
.Filter
46+
.ByExcluding(Matching.FromSource("QuickFix.SessionLogs"))
47+
.WriteTo.File("log/Non-Session-Log.event.current.log")
48+
)
49+
.CreateLogger();
50+
51+
try
52+
{
53+
Log.Information("Starting host");
54+
55+
var builder = Host.CreateApplicationBuilder(args);
56+
builder.Services.AddHostedService<FixBackgroundService>();
57+
builder.Services.AddSerilog();
58+
builder.Services.Configure<AppSettings>(builder.Configuration);
59+
var app = builder.Build();
60+
61+
await app.RunAsync();
62+
}
63+
catch (Exception ex)
64+
{
65+
Log.Fatal(ex, "Host terminated unexpectedly");
66+
}
67+
finally
68+
{
69+
await Log.CloseAndFlushAsync();
70+
}
71+
}
72+
73+
private static string GetSessionIdFromSourceContext(string sourceContext)
74+
{
75+
// Log source is "QuickFix.SessionLogs.<session-id>", this returns <session-id>
76+
var i = sourceContext.IndexOf('.', sourceContext.IndexOf('.') + 1) + 1;
77+
return sourceContext[i..];
78+
}
79+
}

0 commit comments

Comments
 (0)