Skip to content

Commit

Permalink
Merge pull request #221 from sungam3r/api
Browse files Browse the repository at this point in the history
Add API approval test
  • Loading branch information
nblumhardt authored Mar 13, 2023
2 parents e51f566 + c102f60 commit 9765013
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 32 deletions.
49 changes: 17 additions & 32 deletions src/Serilog.Extensions.Logging/Extensions/Logging/LevelConvert.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Serilog Contributors
// Copyright 2019 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,23 +34,15 @@ public static class LevelConvert
/// different semantics.</remarks>
public static LogEventLevel ToSerilogLevel(LogLevel logLevel)
{
switch (logLevel)
return logLevel switch
{
case LogLevel.None:
case LogLevel.Critical:
return LogEventLevel.Fatal;
case LogLevel.Error:
return LogEventLevel.Error;
case LogLevel.Warning:
return LogEventLevel.Warning;
case LogLevel.Information:
return LogEventLevel.Information;
case LogLevel.Debug:
return LogEventLevel.Debug;
case LogLevel.Trace:
default:
return LogEventLevel.Verbose;
}
LogLevel.None or LogLevel.Critical => LogEventLevel.Fatal,
LogLevel.Error => LogEventLevel.Error,
LogLevel.Warning => LogEventLevel.Warning,
LogLevel.Information => LogEventLevel.Information,
LogLevel.Debug => LogEventLevel.Debug,
_ => LogEventLevel.Verbose,
};
}

/// <summary>
Expand All @@ -60,21 +52,14 @@ public static LogEventLevel ToSerilogLevel(LogLevel logLevel)
/// <returns>The Microsoft.Extensions.Logging equivalent of <paramref name="logEventLevel"/>.</returns>
public static LogLevel ToExtensionsLevel(LogEventLevel logEventLevel)
{
switch (logEventLevel)
return logEventLevel switch
{
case LogEventLevel.Fatal:
return LogLevel.Critical;
case LogEventLevel.Error:
return LogLevel.Error;
case LogEventLevel.Warning:
return LogLevel.Warning;
case LogEventLevel.Information:
return LogLevel.Information;
case LogEventLevel.Debug:
return LogLevel.Debug;
case LogEventLevel.Verbose:
default:
return LogLevel.Trace;
}
LogEventLevel.Fatal => LogLevel.Critical,
LogEventLevel.Error => LogLevel.Error,
LogEventLevel.Warning => LogLevel.Warning,
LogEventLevel.Information => LogLevel.Information,
LogEventLevel.Debug => LogLevel.Debug,
_ => LogLevel.Trace,
};
}
}
26 changes: 26 additions & 0 deletions test/Serilog.Extensions.Logging.Tests/ApiApprovalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if NET7_0

using PublicApiGenerator;
using Shouldly;
using Xunit;

namespace Serilog.Extensions.Logging.Tests;

public class ApiApprovalTests
{
[Fact]
public void PublicApi_Should_Not_Change_Unintentionally()
{
var assembly = typeof(LoggerSinkConfigurationExtensions).Assembly;
var publicApi = assembly.GeneratePublicApi(
new()
{
IncludeAssemblyAttributes = false,
ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute" },
});

publicApi.ShouldMatchApproved(options => options.WithFilenameGenerator((_, _, fileType, fileExtension) => $"{assembly.GetName().Name!}.{fileType}.{fileExtension}"));
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Serilog.Extensions.Logging
{
public static class LevelConvert
{
public static Microsoft.Extensions.Logging.LogLevel ToExtensionsLevel(Serilog.Events.LogEventLevel logEventLevel) { }
public static Serilog.Events.LogEventLevel ToSerilogLevel(Microsoft.Extensions.Logging.LogLevel logLevel) { }
}
public class LoggerProviderCollection : System.IDisposable
{
public LoggerProviderCollection() { }
public System.Collections.Generic.IEnumerable<Microsoft.Extensions.Logging.ILoggerProvider> Providers { get; }
public void AddProvider(Microsoft.Extensions.Logging.ILoggerProvider provider) { }
public void Dispose() { }
}
public class SerilogLoggerFactory : Microsoft.Extensions.Logging.ILoggerFactory, System.IDisposable
{
public SerilogLoggerFactory(Serilog.ILogger? logger = null, bool dispose = false, Serilog.Extensions.Logging.LoggerProviderCollection? providerCollection = null) { }
public void AddProvider(Microsoft.Extensions.Logging.ILoggerProvider provider) { }
public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName) { }
public void Dispose() { }
}
[Microsoft.Extensions.Logging.ProviderAlias("Serilog")]
public class SerilogLoggerProvider : Microsoft.Extensions.Logging.ILoggerProvider, Serilog.Core.ILogEventEnricher, System.IDisposable
{
public SerilogLoggerProvider(Serilog.ILogger? logger = null, bool dispose = false) { }
public System.IDisposable BeginScope<T>(T state) { }
public Microsoft.Extensions.Logging.ILogger CreateLogger(string name) { }
public void Dispose() { }
public void Enrich(Serilog.Events.LogEvent logEvent, Serilog.Core.ILogEventPropertyFactory propertyFactory) { }
}
}
namespace Serilog
{
public static class LoggerSinkConfigurationExtensions
{
public static Serilog.LoggerConfiguration Providers(this Serilog.Configuration.LoggerSinkConfiguration configuration, Serilog.Extensions.Logging.LoggerProviderCollection providers, Serilog.Events.LogEventLevel restrictedToMinimumLevel = 0, Serilog.Core.LoggingLevelSwitch? levelSwitch = null) { }
}
public static class SerilogLoggerFactoryExtensions
{
public static Microsoft.Extensions.Logging.ILoggerFactory AddSerilog(this Microsoft.Extensions.Logging.ILoggerFactory factory, Serilog.ILogger? logger = null, bool dispose = false) { }
}
public static class SerilogLoggingBuilderExtensions
{
public static Microsoft.Extensions.Logging.ILoggingBuilder AddSerilog(this Microsoft.Extensions.Logging.ILoggingBuilder builder, Serilog.ILogger? logger = null, bool dispose = false) { }
}
}

0 comments on commit 9765013

Please sign in to comment.