Skip to content

Commit 16be480

Browse files
authored
Merge pull request #202 from serilog/dev
3.4.0 Release
2 parents 210a242 + d05c38c commit 16be480

19 files changed

+295
-62
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
name: Bug report
3+
about: Report a bug and help us to improve Serilog
4+
title: ''
5+
labels: bug
6+
assignees: ''
7+
8+
---
9+
10+
The Serilog maintainers want you to have a great experience using Serilog, and will happily track down and resolve bugs. We all have limited time, though, so please think through all of the factors that might be involved and include as much useful information as possible 😊.
11+
12+
ℹ If the problem is caused by a sink or other related package, please try to track down the correct repository for that package and create the report there: this tracker is for the **Serilog.AspNetCore** package only.
13+
14+
**Description**
15+
What's going wrong?
16+
17+
**Reproduction**
18+
Please provide code samples showing how you're configuring and calling Serilog to produce the behavior.
19+
20+
**Expected behavior**
21+
A concise description of what you expected to happen.
22+
23+
**Relevant package, tooling and runtime versions**
24+
What Serilog version are you using, on what platform?
25+
26+
**Additional context**
27+
Add any other context about the problem here.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an improvement to Serilog
4+
title: ''
5+
labels: enhancement
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/ISSUE_TEMPLATE/usage-help.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
name: Usage help
3+
about: Get help with using Serilog
4+
title: ''
5+
labels: invalid
6+
assignees: ''
7+
8+
---
9+
10+
Hi! 👋
11+
12+
The Serilog community wants you to have a great experience using Serilog. Unfortunately, only a handful of maintainers actively follow this repository, and our time is short, so we cannot answer usage questions posted here.
13+
14+
Fortunately, a much larger group of people (including some of us) also watch and answer questions on the [`serilog` tag on Stack Overflow](https://stackoverflow.com/questions/tagged/serilog).
15+
16+
Please head over to Stack Overflow, ask your question, and tag it with `serilog`. Thanks! ❤

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Program
3131
try
3232
{
3333
Log.Information("Starting web host");
34-
CreateWebHostBuilder(args).Build().Run();
34+
CreateHostBuilder(args).Build().Run();
3535
return 0;
3636
}
3737
catch (Exception ex)
@@ -46,21 +46,22 @@ public class Program
4646
}
4747
```
4848

49-
**Then**, add `UseSerilog()` to the web host builder in `BuildWebHost()`.
49+
**Then**, add `UseSerilog()` to the Generic Host in `CreateHostBuilder()`.
5050

5151
```csharp
52-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
53-
WebHost.CreateDefaultBuilder(args)
54-
.UseStartup<Startup>()
55-
.UseSerilog(); // <-- Add this line;
52+
public static IHostBuilder CreateHostBuilder(string[] args) =>
53+
Host.CreateDefaultBuilder(args)
54+
.UseSerilog() // <-- Add this line
55+
.ConfigureWebHostDefaults(webBuilder =>
56+
{
57+
webBuilder.UseStartup<Startup>();
58+
});
5659
}
5760
```
5861

5962
**Finally**, clean up by removing the remaining configuration for the default logger:
6063

61-
* Remove calls to `AddLogging()`
6264
* Remove the `"Logging"` section from _appsettings.json_ files (this can be replaced with [Serilog configuration](https://github.com/serilog/serilog-settings-configuration) as shown in [the _EarlyInitializationSample_ project](https://github.com/serilog/serilog-aspnetcore/blob/dev/samples/EarlyInitializationSample/Program.cs), if required)
63-
* Remove `ILoggerFactory` parameters and any `Add*()` calls on the logger factory in _Startup.cs_
6465
* Remove `UseApplicationInsights()` (this can be replaced with the [Serilog AI sink](https://github.com/serilog/serilog-sinks-applicationinsights), if required)
6566

6667
That's it! With the level bumped up a little you will see log output resembling:
@@ -191,13 +192,13 @@ app.UseSerilogRequestLogging(options =>
191192
You can alternatively configure Serilog inline, in `BuildWebHost()`, using a delegate as shown below:
192193

193194
```csharp
194-
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
195+
.UseSerilog((hostingContext, services, loggerConfiguration) => loggerConfiguration
195196
.ReadFrom.Configuration(hostingContext.Configuration)
196197
.Enrich.FromLogContext()
197198
.WriteTo.Console())
198199
```
199200

200-
This has the advantage of making the `hostingContext`'s `Configuration` object available for [configuration of the logger](https://github.com/serilog/serilog-settings-configuration), but at the expense of losing `Exception`s raised earlier in program startup.
201+
This has the advantage of making a service provider and the `hostingContext`'s `Configuration` object available for [configuration of the logger](https://github.com/serilog/serilog-settings-configuration), but at the expense of losing `Exception`s raised earlier in program startup.
201202

202203
If this method is used, `Log.Logger` is assigned implicitly, and closed when the app is shut down.
203204

appveyor.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2017
3+
image: Visual Studio 2019
44
install:
55
- ps: ./Setup.ps1
66
build_script:
@@ -10,9 +10,9 @@ artifacts:
1010
- path: artifacts/Serilog.*.nupkg
1111
deploy:
1212
- provider: NuGet
13-
api_key:
14-
secure: gvYNwOSxj9Bq4erOm7alpWzHlEmNLLUV99VYKV1WF9qTJeDkvYpswoNHi9sAlZH4
1513
skip_symbols: true
14+
api_key:
15+
secure: jZtLAmD4ALF9x1BZR1DiV3KhKlliWbzRUAw73xfMZaBsvz19123qLz2zw1+hPdcn
1616
on:
1717
branch: /^(master|dev)$/
1818
- provider: GitHub

global.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"sdk": {
3-
"version": "3.0.100"
3+
"allowPrerelease": false,
4+
"version": "3.1.201",
5+
"rollForward": "latestFeature"
46
}
5-
}
7+
}

samples/EarlyInitializationSample/EarlyInitializationSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

samples/InlineInitializationSample/InlineInitializationSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

samples/InlineInitializationSample/Program.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ public static void Main(string[] args)
1313

1414
public static IHostBuilder CreateHostBuilder(string[] args) =>
1515
Host.CreateDefaultBuilder(args)
16-
.ConfigureWebHostDefaults(webBuilder =>
17-
{
18-
webBuilder.UseStartup<Startup>();
19-
})
20-
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
16+
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
17+
.UseSerilog((hostingContext, services, loggerConfiguration) => loggerConfiguration
2118
.ReadFrom.Configuration(hostingContext.Configuration)
2219
.Enrich.FromLogContext()
2320
.WriteTo.Debug()

samples/InlineInitializationSample/Startup.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
using System.Net;
21
using Microsoft.AspNetCore.Builder;
32
using Microsoft.AspNetCore.Hosting;
43
using Microsoft.Extensions.DependencyInjection;
54
using Microsoft.Extensions.Hosting;
65
using Serilog;
7-
using Serilog.Events;
6+
using Serilog.AspNetCore;
87

98
namespace InlineInitializationSample
109
{
1110
public class Startup
1211
{
1312
public void ConfigureServices(IServiceCollection services)
1413
{
14+
services.Configure<RequestLoggingOptions>(o =>
15+
{
16+
o.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
17+
{
18+
diagnosticContext.Set("RemoteIpAddress", httpContext.Connection.RemoteIpAddress.MapToIPv4());
19+
};
20+
});
21+
1522
services.AddControllersWithViews();
1623
}
1724

src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424

2525
namespace Serilog.AspNetCore
2626
{
27+
// ReSharper disable once ClassNeverInstantiated.Global
2728
class RequestLoggingMiddleware
2829
{
2930
readonly RequestDelegate _next;
3031
readonly DiagnosticContext _diagnosticContext;
3132
readonly MessageTemplate _messageTemplate;
3233
readonly Action<IDiagnosticContext, HttpContext> _enrichDiagnosticContext;
3334
readonly Func<HttpContext, double, Exception, LogEventLevel> _getLevel;
35+
readonly ILogger _logger;
3436
static readonly LogEventProperty[] NoProperties = new LogEventProperty[0];
3537

3638
public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options)
@@ -42,6 +44,7 @@ public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnost
4244
_getLevel = options.GetLevel;
4345
_enrichDiagnosticContext = options.EnrichDiagnosticContext;
4446
_messageTemplate = new MessageTemplateParser().Parse(options.MessageTemplate);
47+
_logger = options.Logger?.ForContext<RequestLoggingMiddleware>();
4548
}
4649

4750
// ReSharper disable once UnusedMember.Global
@@ -73,7 +76,7 @@ public async Task Invoke(HttpContext httpContext)
7376

7477
bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector, int statusCode, double elapsedMs, Exception ex)
7578
{
76-
var logger = Log.ForContext<RequestLoggingMiddleware>();
79+
var logger = _logger ?? Log.ForContext<RequestLoggingMiddleware>();
7780
var level = _getLevel(httpContext, elapsedMs, ex);
7881

7982
if (!logger.IsEnabled(level)) return false;
@@ -106,7 +109,18 @@ static double GetElapsedMilliseconds(long start, long stop)
106109

107110
static string GetPath(HttpContext httpContext)
108111
{
109-
return httpContext.Features.Get<IHttpRequestFeature>()?.RawTarget ?? httpContext.Request.Path.ToString();
112+
/*
113+
In some cases, like when running integration tests with WebApplicationFactory<T>
114+
the RawTarget returns an empty string instead of null, in that case we can't use
115+
?? as fallback.
116+
*/
117+
var requestPath = httpContext.Features.Get<IHttpRequestFeature>()?.RawTarget;
118+
if (string.IsNullOrEmpty(requestPath))
119+
{
120+
requestPath = httpContext.Request.Path.ToString();
121+
}
122+
123+
return requestPath;
110124
}
111125
}
112126
}

src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Serilog Contributors
1+
// Copyright 2019-2020 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,13 +16,25 @@
1616
using Serilog.Events;
1717
using System;
1818

19+
// ReSharper disable UnusedAutoPropertyAccessor.Global
20+
1921
namespace Serilog.AspNetCore
2022
{
2123
/// <summary>
22-
/// Contains options for the <see cref="Serilog.AspNetCore.RequestLoggingMiddleware"/>.
24+
/// Contains options for the <see cref="RequestLoggingMiddleware"/>.
2325
/// </summary>
2426
public class RequestLoggingOptions
2527
{
28+
const string DefaultRequestCompletionMessageTemplate =
29+
"HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";
30+
31+
static LogEventLevel DefaultGetLevel(HttpContext ctx, double _, Exception ex) =>
32+
ex != null
33+
? LogEventLevel.Error
34+
: ctx.Response.StatusCode > 499
35+
? LogEventLevel.Error
36+
: LogEventLevel.Information;
37+
2638
/// <summary>
2739
/// Gets or sets the message template. The default value is
2840
/// <c>"HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms"</c>. The
@@ -50,6 +62,19 @@ public class RequestLoggingOptions
5062
/// </summary>
5163
public Action<IDiagnosticContext, HttpContext> EnrichDiagnosticContext { get; set; }
5264

53-
internal RequestLoggingOptions() { }
65+
/// <summary>
66+
/// The logger through which request completion events will be logged. The default is to use the
67+
/// static <see cref="Log"/> class.
68+
/// </summary>
69+
public ILogger Logger { get; set; }
70+
71+
/// <summary>
72+
/// Constructor
73+
/// </summary>
74+
public RequestLoggingOptions()
75+
{
76+
GetLevel = DefaultGetLevel;
77+
MessageTemplate = DefaultRequestCompletionMessageTemplate;
78+
}
5479
}
5580
}

src/Serilog.AspNetCore/Serilog.AspNetCore.csproj

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
<PropertyGroup>
44
<Description>Serilog support for ASP.NET Core logging</Description>
5-
<VersionPrefix>3.2.0</VersionPrefix>
5+
<VersionPrefix>3.4.0</VersionPrefix>
66
<Authors>Microsoft;Serilog Contributors</Authors>
7-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
7+
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
88
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1010
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
1111
<SignAssembly>true</SignAssembly>
1212
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
1313
<PackageTags>serilog;aspnet;aspnetcore</PackageTags>
14+
<PackageIcon>serilog-extension-nuget.png</PackageIcon>
1415
<PackageIconUrl>https://serilog.net/images/serilog-extension-nuget.png</PackageIconUrl>
1516
<PackageProjectUrl>https://github.com/serilog/serilog-aspnetcore</PackageProjectUrl>
1617
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
@@ -21,16 +22,30 @@
2122
</PropertyGroup>
2223

2324
<ItemGroup>
24-
<PackageReference Include="Serilog" Version="2.8.0" />
25-
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.0.0" />
25+
<None Include="images/serilog-extension-nuget.png" Pack="true" PackagePath="/" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<PackageReference Include="Serilog" Version="2.9.0" />
30+
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.1.0" />
2631
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
27-
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
32+
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
2833
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.1" />
2934
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
30-
<PackageReference Include="Serilog.Formatting.Compact" Version="1.0.0" />
31-
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.0.0" />
32-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
33-
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
35+
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
36+
</ItemGroup>
37+
38+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
39+
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.1" />
40+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
41+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
42+
</ItemGroup>
43+
44+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
45+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
46+
47+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
48+
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
3449
</ItemGroup>
3550

3651
</Project>

0 commit comments

Comments
 (0)