Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to .NET 8 and Serilog v3 #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-16.04
runs-on: ubuntu
name: Build
steps:
# Checkout
- uses: actions/checkout@master
# Setup dotnet 3
# Setup dotnet 8
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.301
dotnet-version: 8.x
# Build
- run: dotnet test -c Release && dotnet build -c Release
################################################
Expand All @@ -32,7 +32,7 @@ jobs:
- name: Pack and Release
id: pack_release
run: |
PACKAGE="Redbox.Serilog.Stackdriver"
PACKAGE="Raileasy.Serilog.Stackdriver"
echo ::set-output name=PACKAGE::$PACKAGE
dotnet pack src/$PACKAGE --output nupkgs -p:Version=${{ steps.get_version.outputs.VERSION }}
dotnet nuget push nupkgs/$PACKAGE.*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json
Expand All @@ -47,4 +47,4 @@ jobs:
Install instructions:
`dotnet add package ${{ steps.pack_release.outputs.PACKAGE }} --version ${{ steps.get_version.outputs.VERSION }}`
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Stackdriver Formatter for Serilog

**This is a fork of the original Redbox project, updated for Serilog v3 and with a .NET 8.0 example.**

A Stackdriver JSON Formatter for logging with Serilog and .NET. Useful when a dependency on the Google SDK is not wanted or when a logs are being sent to Stackdriver using a data collector or log shipper (e.g. Fluentd).

## Serilog Sinks
Expand All @@ -8,18 +10,18 @@ There is no dependency on any particular Serilog Sinks. Pass in an instance of

## Installing

A `netstandard2.0` Nuget package is available [here](https://www.nuget.org/packages/Redbox.Serilog.Stackdriver/).
A `netstandard2.0` Nuget package is available [here](https://www.nuget.org/packages/Raileasy.Serilog.Stackdriver/).

Or you can install with the dotnet cli:

`dotnet add package Redbox.Serilog.Stackdriver`
`dotnet add package Raileasy.Serilog.Stackdriver`

## Sample Setup Code

### Directly into a Serilog Instance

```csharp
using Redbox.Serilog.Stackdriver
using Raileasy.Serilog.Stackdriver

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
Expand All @@ -42,7 +44,7 @@ Be sure to add `.ReadFrom.Configuration(configuration)` to your Serilog setup fi
{
"Name": "Console",
"Args": {
"formatter": "Redbox.Serilog.Stackdriver.StackdriverJsonFormatter, Redbox.Serilog.Stackdriver"
"formatter": "Raileasy.Serilog.Stackdriver.StackdriverJsonFormatter, Raileasy.Serilog.Stackdriver"
}
}]
}
Expand Down
6 changes: 5 additions & 1 deletion src/Debugger/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public IEnumerable<WeatherForecast> Get()
})
.ToArray();

_logger.LogInformation($"Returning some weather!");
var maxTemp = weather.Max(w => w.TemperatureC);
using (var scope = _logger.BeginScope("{MaximumTemperature}", maxTemp))
{
_logger.LogInformation($"Returning some weather!");
}

return weather;
}
Expand Down
14 changes: 8 additions & 6 deletions src/Debugger/Debugger.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Redbox.Serilog.Stackdriver\Redbox.Serilog.Stackdriver.csproj" />
<ProjectReference Include="..\Raileasy.Serilog.Stackdriver\Raileasy.Serilog.Stackdriver.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<ItemGroup>
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
</ItemGroup>

</Project>
88 changes: 33 additions & 55 deletions src/Debugger/Program.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Redbox.Serilog.Stackdriver;

namespace Debugger
var builder = WebApplication.CreateBuilder(args);

Serilog.Debugging.SelfLog.Enable(Console.WriteLine);

builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
public class Program
{
public static int Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.ReadFrom.Configuration(configuration)
.WriteTo.Console(new StackdriverJsonFormatter())
.CreateLogger();

Log.Logger.Information("test");

try
{
Log.Information("Starting web host");
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}


public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseSerilog();
});
}
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
50 changes: 0 additions & 50 deletions src/Debugger/Startup.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Debugger/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
"Name": "Console",
"Args": {
"formatter": "Redbox.Serilog.Stackdriver.StackdriverJsonFormatter, Redbox.Serilog.Stackdriver"
"formatter": "Raileasy.Serilog.Stackdriver.StackdriverJsonFormatter, Raileasy.Serilog.Stackdriver"
}
}
],
Expand All @@ -29,4 +29,4 @@
"Application": "Sample"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Redbox.Serilog.Stackdriver\Redbox.Serilog.Stackdriver.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Raileasy.Serilog.Stackdriver\Raileasy.Serilog.Stackdriver.csproj" />
</ItemGroup>

</Project>
Loading