Skip to content

Commit

Permalink
Merge pull request #85 from gertjvr/dev
Browse files Browse the repository at this point in the history
Extends ILoggerBuilder this resolves #84
  • Loading branch information
nblumhardt authored Jul 18, 2017
2 parents 7a92b2f + 53a914f commit 86aeba9
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 46 deletions.
10 changes: 5 additions & 5 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2015
image: Visual Studio 2017 Preview
configuration: Release
install:
- ps: mkdir -Force ".\build\" | Out-Null
- ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/release/2.0.0-preview2/scripts/obtain/dotnet-install.ps1" -OutFile ".\build\installcli.ps1"
- ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli"
- ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 2.0.0-preview2-006497'
- ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
#- ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/release/2.0.0-preview2/scripts/obtain/dotnet-install.ps1" -OutFile ".\build\installcli.ps1"
#- ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli"
#- ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 2.0.0-preview2-006497'
#- ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
build_script:
- ps: ./Build.ps1
test: off
Expand Down
17 changes: 11 additions & 6 deletions samples/Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;

namespace Sample
{
public static class Program
public class Program
{
public static void Main(string[] args)
{
Expand All @@ -13,18 +14,22 @@ public static void Main(string[] args)
.WriteTo.LiterateConsole()
.CreateLogger();

var logger = new LoggerFactory()
.AddSerilog()
.CreateLogger(typeof(Program).FullName);
var services = new ServiceCollection()
.AddLogging(builder =>
{
builder.AddSerilog();
});

logger.LogInformation("Starting");
var serviceProvider = services.BuildServiceProvider();
// getting the logger using the class's name is conventional
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();

var startTime = DateTimeOffset.UtcNow;
logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);

try
{
throw new Exception("Boom");
throw new Exception("Boom!");
}
catch (Exception ex)
{
Expand Down
10 changes: 5 additions & 5 deletions samples/Sample/Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net46;netcoreapp1.0</TargetFrameworks>
<TargetFrameworks>net46;netcoreapp2.0</TargetFrameworks>
<AssemblyName>Sample</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>Sample</PackageId>
<RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">1.0.4</RuntimeFrameworkVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0-preview2-final" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0-preview2-final" />
<PackageReference Include="Serilog.Sinks.Literate" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

</Project>
</Project>
9 changes: 6 additions & 3 deletions samples/WebSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ public Startup(IHostingEnvironment env)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(builder =>
{
// Specifying dispose: true closes and flushes the Serilog `Log` class when the app shuts down.
builder.AddSerilog(dispose: true);
});

// Add framework services.
services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Specifying dispose: true closes and flushes the Serilog `Log` class when the app shuts down.
loggerFactory.AddSerilog(dispose: true);

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand Down
13 changes: 3 additions & 10 deletions samples/WebSample/WebSample.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>WebSample</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>WebSample</PackageId>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
</PropertyGroup>

<ItemGroup>
<None Update="wwwroot\**\*;Views\**\*;Areas\**\Views">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
Expand All @@ -27,7 +23,7 @@
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0-preview2-final" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.2" />
Expand All @@ -37,14 +33,11 @@
<PackageReference Include="Serilog.Sinks.Literate" Version="2.1.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="3.2.0" />
</ItemGroup>

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
<Exec Command="bower install" />
<Exec Command="dotnet bundle" />
</Target>

<ItemGroup>
<DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.2.301" />
</ItemGroup>

</Project>
</Project>
3 changes: 2 additions & 1 deletion serilog-extensions-logging.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26114.2
Expand All @@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9
Build.ps1 = Build.ps1
README.md = README.md
assets\Serilog.snk = assets\Serilog.snk
global.json = global.json
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebSample", "samples\WebSample\WebSample.csproj", "{486DF7EB-128D-4C79-8B97-9CEFEADDB948}"
Expand Down
11 changes: 5 additions & 6 deletions src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,24 @@
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.0" />
<Compile Remove="SerilogLoggerServicesExtensions.cs" />
<Compile Remove="SerilogLoggingBuilderExtensions.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System.Runtime" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.0" />
<Compile Remove="SerilogLoggerServicesExtensions.cs" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0-preview2-final" />
<PackageReference Include="NETStandard.Library" Version="2.0.0-preview2-25401-01" />
<Compile Remove="SerilogLoggerFactoryExtensions.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.0" />
<Compile Remove="SerilogLoggerServicesExtensions.cs" />
<Compile Remove="SerilogLoggingBuilderExtensions.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0-preview2-final" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0-preview2-final" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0-preview2-final" />
<PackageReference Include="NETStandard.Library" Version="2.0.0-preview2-25401-01" />
<Compile Remove="SerilogLoggerFactoryExtensions.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
using System;
using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog.Extensions.Logging;


namespace Serilog
{
/// <summary>
/// Extends <see cref="IServiceCollection"/> with Serilog configuration methods.
/// Extends <see cref="ILoggingBuilder"/> with Serilog configuration methods.
/// </summary>
public static class SerilogLoggerServicesExtensions
public static class SerilogLoggingBuilderExtensions
{
/// <summary>
/// Add Serilog to the logging pipeline.
/// </summary>
/// <param name="services">The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /> to add services to.</param>
/// <param name="builder">The <see cref="T:Microsoft.Extensions.Logging.ILoggingBuilder" /> to add logging provider to.</param>
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
/// <param name="dispose">When true, dispose <paramref name="logger"/> when the framework disposes the provider. If the
/// logger is not specified but <paramref name="dispose"/> is true, the <see cref="Log.CloseAndFlush()"/> method will be
/// called on the static <see cref="Log"/> class instead.</param>
/// <returns>The logger factory.</returns>
public static IServiceCollection AddSerilog(this IServiceCollection services, Serilog.ILogger logger = null, bool dispose = false)
public static void AddSerilog(this ILoggingBuilder builder, ILogger logger = null, bool dispose = false)
{
if (services == null) throw new ArgumentNullException(nameof(services));
if (builder == null) throw new ArgumentNullException(nameof(builder));

services.AddLogging(builder => builder.AddProvider(new SerilogLoggerProvider(logger, dispose)));

return services;
builder.AddProvider(new SerilogLoggerProvider(logger, dispose));
}
}
}

0 comments on commit 86aeba9

Please sign in to comment.