Skip to content

Commit 66505c7

Browse files
committed
Logger config tests need work but basic coverage is in place
1 parent fddd16b commit 66505c7

22 files changed

+599
-15
lines changed

src/AStar.Dev.Logging.Extensions/AStar.Dev.Logging.Extensions.csproj

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
45
<TargetFramework>net8.0</TargetFramework>
56
<ImplicitUsings>enable</ImplicitUsings>
67
<Nullable>enable</Nullable>
@@ -17,6 +18,7 @@
1718

1819
<PropertyGroup>
1920
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
21+
<IsPackable>true</IsPackable>
2022
<Title>AStar.Dev.Logging.Extensions</Title>
2123
<Company>AStar Development</Company>
2224
<Copyright>AStar Development, 2024</Copyright>
@@ -25,9 +27,9 @@
2527
<IncludeSymbols>True</IncludeSymbols>
2628
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2729
<GenerateDocumentationFile>True</GenerateDocumentationFile>
28-
<RepositoryUrl>https://github.com/jbarden/astar-dev-logging-extensions.git</RepositoryUrl>
30+
<RepositoryUrl>https://github.com/astar-development/astar-dev-logging-extensions.git</RepositoryUrl>
2931
<RepositoryType>git</RepositoryType>
30-
<PackageProjectUrl>https://github.com/jbarden/astar-dev-logging-extensions</PackageProjectUrl>
32+
<PackageProjectUrl>https://github.com/astar-development/astar-dev-logging-extensions</PackageProjectUrl>
3133
<Description>A collection of logging utilities.</Description>
3234
<Version>0.4.0</Version>
3335
<Authors>AStar Development, Jason Barden</Authors>
@@ -37,6 +39,18 @@
3739
<PackageReleaseNotes>version 0.4.0, no changes - version increased as part of the migration to the new AStar NuGet / GitHub organisations.</PackageReleaseNotes>
3840
</PropertyGroup>
3941

42+
<ItemGroup>
43+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
44+
<PackageReference Include="AStar.Dev.Utilities" Version="1.5.1" />
45+
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
46+
<PackageReference Include="Serilog.Enrichers.Span" Version="3.1.0" />
47+
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
48+
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />
49+
<PackageReference Include="Serilog.Sinks.Seq" Version="8.0.0" />
50+
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
51+
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
52+
</ItemGroup>
53+
4054
<ItemGroup>
4155
<None Include="..\..\AStar.png">
4256
<Pack>True</Pack>

src/AStar.Dev.Logging.Extensions/AStar.Dev.Logging.Extensions.xml

Lines changed: 221 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using AStar.Dev.Logging.Extensions.Models;
2+
using AStar.Dev.Utilities;
3+
using Microsoft.ApplicationInsights.Extensibility;
4+
using Serilog;
5+
6+
namespace AStar.Dev.Logging.Extensions;
7+
8+
/// <summary>
9+
/// The <see cref="LoggingExtensions" /> class contains, as you might expect, extension methods for configuring Serilog / Application Insights.
10+
/// </summary>
11+
public static class LoggingExtensions
12+
{
13+
/// <summary>
14+
/// The <see cref="AddSerilogLogging" /> method will add Serilog to the logging providers.
15+
/// </summary>
16+
/// <param name="builder">
17+
/// </param>
18+
/// <param name="externalSettingsFile">
19+
/// The name (including extension) of the file containing the Serilog Configuration settings.
20+
/// </param>
21+
/// <returns>
22+
/// The original instance of <see cref="WebApplicationBuilder" /> for further method chaining.
23+
/// </returns>
24+
public static WebApplicationBuilder AddSerilogLogging(this WebApplicationBuilder builder, string externalSettingsFile = "")
25+
{
26+
if(externalSettingsFile.IsNotNullOrWhiteSpace())
27+
{
28+
_ = builder.Configuration.AddJsonFile(path: externalSettingsFile, optional: false, reloadOnChange: true);
29+
}
30+
31+
_ = builder.Configuration.AddUserSecrets<Program>();
32+
33+
_ = builder.Services.AddApplicationInsightsTelemetry();
34+
var serviceProvider = builder.Services.BuildServiceProvider();
35+
var seqServerUrl = builder.Configuration.Get<SerilogConfig>()!.Serilog.WriteTo[0].Args.ServerUrl;
36+
var logger = new LoggerConfiguration()
37+
.WriteTo.ApplicationInsights(serviceProvider.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces)
38+
.WriteTo.Console()
39+
.WriteTo.Seq(seqServerUrl)
40+
.CreateLogger();
41+
42+
logger.Debug("Serilog has been configured.");
43+
44+
Log.Logger = logger;
45+
46+
_ = builder.Host
47+
.UseSerilog((context, loggerConfig) => loggerConfig
48+
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {Message:lj}{NewLine}{Exception}")
49+
.ReadFrom.Configuration(context.Configuration)
50+
.Enrich.FromLogContext());
51+
52+
return builder;
53+
}
54+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace AStar.Dev.Logging.Extensions.Models;
2+
3+
/// <summary>
4+
/// The <see cref="ApplicationInsights"/> class which is used to configure the logging.
5+
/// </summary>
6+
public class ApplicationInsights
7+
{
8+
/// <summary>
9+
/// The Log level to use.
10+
/// </summary>
11+
public LogLevel LogLevel { get; set; } = new();
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace AStar.Dev.Logging.Extensions.Models;
2+
3+
/// <summary>
4+
/// The <see cref="Args"/> class used by Serilog WriteTo section of the logging configuration.
5+
/// </summary>
6+
public class Args
7+
{
8+
/// <summary>
9+
/// The Serilog server Url.
10+
/// </summary>
11+
public string ServerUrl { get; set; } = string.Empty;
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace AStar.Dev.Logging.Extensions.Models;
2+
3+
/// <summary>
4+
/// The <see cref="Console"/> class used by Serilog Console section of the logging configuration.
5+
/// </summary>
6+
public class Console
7+
{
8+
/// <summary>
9+
/// The Formatter Name to use.
10+
/// </summary>
11+
public string FormatterName { get; set; } = string.Empty;
12+
13+
/// <summary>
14+
/// The <see cref="FormatterOptions"/> class used to supply the Formatter Options to use.
15+
/// </summary>
16+
public FormatterOptions FormatterOptions { get; set; } = new();
17+
}

0 commit comments

Comments
 (0)