Skip to content

Commit

Permalink
Server: Service: use Serilog rolling file for logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xian55 committed Oct 22, 2022
1 parent 0202057 commit dc586a6
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
1 change: 1 addition & 0 deletions Server/ComplexPrototypeSystem.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="3.2.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="SuperSimpleTcp" Version="3.0.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.4.0" />
Expand Down
24 changes: 12 additions & 12 deletions Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using Serilog;

namespace ComplexPrototypeSystem.Server
{
Expand All @@ -19,9 +14,14 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
18 changes: 17 additions & 1 deletion Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,21 @@
"Address": "0.0.0.0",
"Port": "9000"
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "./logs/log-.log",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3}] {Username} {Message:lj}{NewLine}{Exception}"
}
}
]
}
}
1 change: 1 addition & 0 deletions Service/ComplexPrototypeSystem.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="6.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="SuperSimpleTcp" Version="3.0.0.2" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.1" />
</ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.EventLog;

using Serilog;

namespace ComplexPrototypeSystem.Service
{
public class Program
Expand All @@ -27,6 +29,13 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(AppDomain.CurrentDomain.BaseDirectory + "\\logs\\log.log",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}"))
.UseWindowsService(options =>
{
options.ServiceName = "CPU Sensor Service";
Expand Down
2 changes: 1 addition & 1 deletion Service/Worker/CPUInfoCollectorWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var now = DateTime.UtcNow;

logger.LogInformation($"{now} - Temp:{tempF} Usage:{usage}");
logger.LogInformation($"Temp:{tempF} Usage:{usage}");

bw.Write((byte)Opcode.Report);
bw.Write(sizeof(long) + sizeof(int) + sizeof(int));
Expand Down
16 changes: 16 additions & 0 deletions Service/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,21 @@
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "./logs/log-.log",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3}] {Username} {Message:lj}{NewLine}{Exception}"
}
}
]
}
}

0 comments on commit dc586a6

Please sign in to comment.