Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit f0a39c9

Browse files
better logging
1 parent 87f64b9 commit f0a39c9

3 files changed

Lines changed: 39 additions & 20 deletions

File tree

src/KestrelPureOwin/KestrelOwinServer.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public KestrelOwinServer(KestrelServerOptions options)
4949
}
5050

5151
var wrappedOptions = Options.Create(options);
52-
52+
5353
var loggerFactory = new LoggerFactory();
54+
loggerFactory.AddProvider(new ConsoleLogProvider());
5455

5556
var transport = new LibuvTransportFactory(
5657
Options.Create(new LibuvTransportOptions()),
@@ -65,18 +66,6 @@ private static KestrelServerOptions GetOptions(IDictionary<string, object> prope
6566

6667
public KestrelServer Server { get; }
6768

68-
public Task Run(string url, Action<BuildFunc> configure, CancellationToken cancellationToken)
69-
{
70-
Console.CancelKeyPress += (s, e) =>
71-
{
72-
Console.WriteLine("Application is shutting down...");
73-
74-
e.Cancel = true;
75-
};
76-
77-
return Start(url, configure, cancellationToken);
78-
}
79-
8069
public Task Start(string url, Action<BuildFunc> configure, CancellationToken cancellationToken)
8170
{
8271
var application = ConfigureApplication(configure);
@@ -119,5 +108,32 @@ private static OwinApplication BuildApplication(IEnumerable<MidFunc> middleware)
119108

120109
return new OwinApplication(app);
121110
}
111+
112+
private class ConsoleLogger : ILogger
113+
{
114+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
115+
=> Console.WriteLine(formatter(state, exception));
116+
117+
public bool IsEnabled(LogLevel logLevel) => true;
118+
119+
public IDisposable BeginScope<TState>(TState state) => new NoOpScope();
120+
121+
private class NoOpScope : IDisposable
122+
{
123+
public void Dispose()
124+
{
125+
}
126+
}
127+
}
128+
129+
private class ConsoleLogProvider : ILoggerProvider
130+
{
131+
public void Dispose()
132+
{
133+
134+
}
135+
136+
public ILogger CreateLogger(string categoryName) => new ConsoleLogger();
137+
}
122138
}
123139
}

src/SqlStreamStore.HAL.DevServer/Program.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ private static Action<BuildFunc> Configure(IStreamStore streamStore)
5959
.Use(DisplayErrors)
6060
.Use(SqlStreamStoreHalMiddleware.UseSqlStreamStoreHal(streamStore));
6161

62-
private static MidFunc DisplayErrors => next => env => next(env).ContinueWith(_ =>
62+
private static MidFunc DisplayErrors => next => async env =>
63+
{
64+
try
65+
{
66+
await next(env);
67+
}
68+
catch(Exception ex)
6369
{
64-
Console.WriteLine(_.Exception);
65-
66-
return Task.CompletedTask;
67-
},
68-
TaskContinuationOptions.OnlyOnFaulted);
70+
Console.WriteLine(ex);
71+
}
72+
};
6973

7074
private static void DisplayMenu(IStreamStore streamStore)
7175
{

src/SqlStreamStore.HAL/StreamResource.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public async Task<Response> GetMessage(
7272
};
7373
}
7474

75-
7675
var payload = await message.GetJsonData(cancellationToken);
7776

7877
return new Response(

0 commit comments

Comments
 (0)