Skip to content

Commit c9f04a3

Browse files
authored
Merge pull request #238 from ckadluba/dev
New sample, code of conduct.
2 parents 482ed08 + 0be32bb commit c9f04a3

File tree

9 files changed

+154
-0
lines changed

9 files changed

+154
-0
lines changed

CODE_OF_CONDUCT.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code of Conduct
2+
3+
Please refer to the [Serilog Code of Conduct](https://github.com/serilog/serilog/blob/dev/CODE_OF_CONDUCT.md) which covers all repositories within the Serilog Organization.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Serilog.Events;
2+
using Serilog.Formatting;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace WorkerServiceDemo
7+
{
8+
public class CustomLogEventFormatter : ITextFormatter
9+
{
10+
public static CustomLogEventFormatter Formatter { get; } = new CustomLogEventFormatter();
11+
12+
public void Format(LogEvent logEvent, TextWriter output)
13+
{
14+
logEvent.Properties.ToList()
15+
.ForEach(e => output.Write($"{e.Key}={e.Value} "));
16+
}
17+
}
18+
}

sample/WorkerServiceDemo/Program.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using Serilog;
4+
5+
namespace WorkerServiceDemo
6+
{
7+
public class Program
8+
{
9+
public static void Main(string[] args)
10+
{
11+
CreateHostBuilder(args).Build().Run();
12+
}
13+
14+
public static IHostBuilder CreateHostBuilder(string[] args) =>
15+
Host.CreateDefaultBuilder(args)
16+
.ConfigureServices((hostContext, services) =>
17+
{
18+
services.AddHostedService<Worker>();
19+
})
20+
.UseSerilog((hostingContext, loggerConfiguration) =>
21+
{
22+
loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);
23+
});
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"WorkerServiceDemo": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"DOTNET_ENVIRONMENT": "Development"
7+
}
8+
}
9+
}
10+
}

sample/WorkerServiceDemo/Worker.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace WorkerServiceDemo
8+
{
9+
public class Worker : BackgroundService
10+
{
11+
private readonly ILogger<Worker> _logger;
12+
13+
public Worker(ILogger<Worker> logger)
14+
{
15+
_logger = logger;
16+
}
17+
18+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
19+
{
20+
_logger.LogInformation("Worker started");
21+
22+
while (!stoppingToken.IsCancellationRequested)
23+
{
24+
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
25+
await Task.Delay(1000, stoppingToken);
26+
}
27+
28+
_logger.LogInformation("Worker stopping ...");
29+
}
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<UserSecretsId>dotnet-WorkerServiceDemo-A4DFF8A6-AC69-443B-A3B8-34E284CD1C78</UserSecretsId>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.2" />
10+
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.0.0" />
11+
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\src\Serilog.Sinks.MSSqlServer\Serilog.Sinks.MSSqlServer.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"Serilog": {
10+
"Using": [ "Serilog.Sinks.MSSqlServer" ],
11+
"MinimumLevel": "Debug",
12+
"WriteTo": [
13+
{
14+
"Name": "MSSqlServer",
15+
"Args": {
16+
"connectionString": "Server=localhost;Database=LogTest;Integrated Security=SSPI;",
17+
"tableName": "LogEvents",
18+
"columnOptionsSection": {
19+
"addStandardColumns": [ "LogEvent" ],
20+
"removeStandardColumns": [ "MessageTemplate", "Properties" ],
21+
"timeStamp": {
22+
"columnName": "Timestamp",
23+
"convertToUtc": false
24+
}
25+
},
26+
"autoCreateSqlTable": true,
27+
"restrictedToMinimumLevel": "Information",
28+
"logEventFormatter": "WorkerServiceDemo.CustomLogEventFormatter::Formatter, WorkerServiceDemo"
29+
}
30+
}
31+
]
32+
}
33+
}

serilog-sinks-mssqlserver.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomLogEventFormatterDemo
2525
EndProject
2626
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppConfigDemo", "sample\AppConfigDemo\AppConfigDemo.csproj", "{7DC530B1-68FD-4F07-A2F9-910C338562C1}"
2727
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerServiceDemo", "sample\WorkerServiceDemo\WorkerServiceDemo.csproj", "{04F523D9-F00B-4C63-9287-31A244378E06}"
29+
EndProject
2830
Global
2931
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3032
Debug|Any CPU = Debug|Any CPU
@@ -47,6 +49,10 @@ Global
4749
{7DC530B1-68FD-4F07-A2F9-910C338562C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
4850
{7DC530B1-68FD-4F07-A2F9-910C338562C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
4951
{7DC530B1-68FD-4F07-A2F9-910C338562C1}.Release|Any CPU.Build.0 = Release|Any CPU
52+
{04F523D9-F00B-4C63-9287-31A244378E06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
53+
{04F523D9-F00B-4C63-9287-31A244378E06}.Debug|Any CPU.Build.0 = Debug|Any CPU
54+
{04F523D9-F00B-4C63-9287-31A244378E06}.Release|Any CPU.ActiveCfg = Release|Any CPU
55+
{04F523D9-F00B-4C63-9287-31A244378E06}.Release|Any CPU.Build.0 = Release|Any CPU
5056
EndGlobalSection
5157
GlobalSection(SolutionProperties) = preSolution
5258
HideSolutionNode = FALSE
@@ -56,6 +62,7 @@ Global
5662
{3C2D8E01-5580-426A-BDD9-EC59CD98E618} = {F02D6513-6F45-452E-85A0-41A872A2C1F8}
5763
{873320F1-8F6D-45E2-A853-D321C86793EC} = {AA346332-5BAF-47F1-B8FB-7600ED61265D}
5864
{7DC530B1-68FD-4F07-A2F9-910C338562C1} = {AA346332-5BAF-47F1-B8FB-7600ED61265D}
65+
{04F523D9-F00B-4C63-9287-31A244378E06} = {AA346332-5BAF-47F1-B8FB-7600ED61265D}
5966
EndGlobalSection
6067
GlobalSection(ExtensibilityGlobals) = postSolution
6168
SolutionGuid = {AAA6BF8D-7B53-4A5F-A79A-D1B306383B45}

0 commit comments

Comments
 (0)