Skip to content

Commit

Permalink
Adding logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Oxtoby authored and soxtoby committed Oct 24, 2021
1 parent 23fcedf commit dc17fb2
Show file tree
Hide file tree
Showing 67 changed files with 3,005 additions and 167 deletions.
6 changes: 5 additions & 1 deletion SlackNet.AspNetCore/AspNetCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ public static IServiceCollection AddSlackNet(this IServiceCollection serviceColl
serviceCollection.TryAddSingleton<ISlackRequestHandler, SlackRequestHandler>();
serviceCollection.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
serviceCollection.TryAddSingleton<IServiceProviderSlackRequestListener, AspNetCoreServiceProviderSlackRequestListener>();
return ServiceCollectionExtensions.AddSlackNet(serviceCollection, configure);
return ServiceCollectionExtensions.AddSlackNet(serviceCollection, c =>
{
c.UseLogger<MicrosoftLoggerAdaptor>();
configure?.Invoke(c);
});
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions SlackNet.AspNetCore/MicrosoftLoggerAdaptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Extensions.Logging;
using SlackNetLogger = SlackNet.ILogger;
using MicrosoftLogger = Microsoft.Extensions.Logging.ILogger;

namespace SlackNet.AspNetCore
{
class MicrosoftLoggerAdaptor : SlackNetLogger
{
private readonly MicrosoftLogger _logger;
public MicrosoftLoggerAdaptor(ILoggerFactory loggerFactory) => _logger = loggerFactory.CreateLogger("SlackNet");

public void Log(ILogEvent logEvent) =>
_logger.Log(logEvent.Category switch
{
LogCategory.Data => LogLevel.Trace,
LogCategory.Internal => LogLevel.Debug,
LogCategory.Request => LogLevel.Information,
LogCategory.Error => LogLevel.Error,
_ => LogLevel.Trace
},
logEvent.FullMessageTemplate(),
logEvent.FullMessagePropertyValues());
}
}
1 change: 1 addition & 0 deletions SlackNet.AspNetCore/SlackNet.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
333 changes: 236 additions & 97 deletions SlackNet.AspNetCore/SlackRequestHandler.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions SlackNet.Autofac/AutofacSlackServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal static void Configure(ContainerBuilder containerBuilder, Action<Autofac
containerBuilder.Register(c => c.Resolve<ISlackServiceProvider>().GetJsonSettings()).As<SlackJsonSettings>().SingleInstance();
containerBuilder.Register(c => c.Resolve<ISlackServiceProvider>().GetTypeResolver()).As<ISlackTypeResolver>().SingleInstance();
containerBuilder.Register(c => c.Resolve<ISlackServiceProvider>().GetUrlBuilder()).As<ISlackUrlBuilder>().SingleInstance();
containerBuilder.Register(c => c.Resolve<ISlackServiceProvider>().GetLogger()).As<ILogger>().SingleInstance();
containerBuilder.Register(c => c.Resolve<ISlackServiceProvider>().GetWebSocketFactory()).As<IWebSocketFactory>().SingleInstance();
containerBuilder.Register(c => c.Resolve<ISlackServiceProvider>().GetRequestListeners()).As<IEnumerable<ISlackRequestListener>>().SingleInstance();
containerBuilder.Register(c => c.Resolve<ISlackServiceProvider>().GetHandlerFactory()).As<ISlackHandlerFactory>().SingleInstance();
Expand Down
1 change: 1 addition & 0 deletions SlackNet.Autofac/AutofacSlackServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public T Resolve<T>(Func<IComponentContext, T> getService)
public SlackJsonSettings GetJsonSettings() => _baseProvider.GetJsonSettings();
public ISlackTypeResolver GetTypeResolver() => _baseProvider.GetTypeResolver();
public ISlackUrlBuilder GetUrlBuilder() => _baseProvider.GetUrlBuilder();
public ILogger GetLogger() => _baseProvider.GetLogger();
public IWebSocketFactory GetWebSocketFactory() => _baseProvider.GetWebSocketFactory();
public IEnumerable<ISlackRequestListener> GetRequestListeners() => _baseProvider.GetRequestListeners();
public ISlackHandlerFactory GetHandlerFactory() => _baseProvider.GetHandlerFactory();
Expand Down
3 changes: 3 additions & 0 deletions SlackNet.EventsExample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public void ConfigureServices(IServiceCollection services)
.RegisterViewSubmissionHandler<WorkflowExample>(WorkflowExample.ConfigCallback)
.RegisterEventHandler<WorkflowStepExecute, WorkflowExample>()
);

services.AddMvc();

services.AddLogging();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
3 changes: 2 additions & 1 deletion SlackNet.EventsExample/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
"Microsoft": "Information",
"SlackNet": "Information"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal static void Configure(IServiceCollection serviceCollection, Action<Serv
serviceCollection.TryAddSingleton<SlackJsonSettings>(sp => sp.GetRequiredService<ISlackServiceProvider>().GetJsonSettings());
serviceCollection.TryAddSingleton<ISlackTypeResolver>(sp => sp.GetRequiredService<ISlackServiceProvider>().GetTypeResolver());
serviceCollection.TryAddSingleton<ISlackUrlBuilder>(sp => sp.GetRequiredService<ISlackServiceProvider>().GetUrlBuilder());
serviceCollection.TryAddSingleton<ILogger>(sp => sp.GetRequiredService<ISlackServiceProvider>().GetLogger());
serviceCollection.TryAddSingleton<IWebSocketFactory>(sp => sp.GetRequiredService<ISlackServiceProvider>().GetWebSocketFactory());
serviceCollection.TryAddSingleton<IEnumerable<ISlackRequestListener>>(sp => sp.GetRequiredService<ISlackServiceProvider>().GetRequestListeners());
serviceCollection.TryAddSingleton<ISlackHandlerFactory>(sp => sp.GetRequiredService<ISlackServiceProvider>().GetHandlerFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ServiceProviderSlackServiceProvider(Func<ISlackServiceProvider, ISlackSer
public SlackJsonSettings GetJsonSettings() => _baseProvider.GetJsonSettings();
public ISlackTypeResolver GetTypeResolver() => _baseProvider.GetTypeResolver();
public ISlackUrlBuilder GetUrlBuilder() => _baseProvider.GetUrlBuilder();
public ILogger GetLogger() => _baseProvider.GetLogger();
public IWebSocketFactory GetWebSocketFactory() => _baseProvider.GetWebSocketFactory();
public IEnumerable<ISlackRequestListener> GetRequestListeners() => _baseProvider.GetRequestListeners();
public ISlackHandlerFactory GetHandlerFactory() => _baseProvider.GetHandlerFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal static void Configure(Container container, Action<SimpleInjectorSlackSe
RegisterFallback<SlackJsonSettings>(container, () => container.GetInstance<ISlackServiceProvider>().GetJsonSettings(), Lifestyle.Singleton);
RegisterFallback<ISlackTypeResolver>(container, () => container.GetInstance<ISlackServiceProvider>().GetTypeResolver(), Lifestyle.Singleton);
RegisterFallback<ISlackUrlBuilder>(container, () => container.GetInstance<ISlackServiceProvider>().GetUrlBuilder(), Lifestyle.Singleton);
RegisterFallback<ILogger>(container, () => container.GetInstance<ISlackServiceProvider>().GetLogger(), Lifestyle.Singleton);
RegisterFallback<IWebSocketFactory>(container, () => container.GetInstance<ISlackServiceProvider>().GetWebSocketFactory(), Lifestyle.Singleton);
RegisterFallback<IEnumerable<ISlackRequestListener>>(container, () => container.GetInstance<ISlackServiceProvider>().GetRequestListeners(), Lifestyle.Singleton);
RegisterFallback<ISlackHandlerFactory>(container, () => container.GetInstance<ISlackServiceProvider>().GetHandlerFactory(), Lifestyle.Singleton);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public SimpleInjectorSlackServiceProvider(Func<ISlackServiceProvider, ISlackServ
public SlackJsonSettings GetJsonSettings() => _baseProvider.GetJsonSettings();
public ISlackTypeResolver GetTypeResolver() => _baseProvider.GetTypeResolver();
public ISlackUrlBuilder GetUrlBuilder() => _baseProvider.GetUrlBuilder();
public ILogger GetLogger() => _baseProvider.GetLogger();
public IWebSocketFactory GetWebSocketFactory() => _baseProvider.GetWebSocketFactory();
public IEnumerable<ISlackRequestListener> GetRequestListeners() => _baseProvider.GetRequestListeners();
public ISlackHandlerFactory GetHandlerFactory() => _baseProvider.GetHandlerFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public void UseUrlBuilderType()
s => s.GetUrlBuilder());
}

[Test]
public void UseLoggerType()
{
UseService<ILogger, TestLogger>(
c => c.UseLogger<TestLogger>(),
s => s.GetLogger());
}

[Test]
public void UseWebSocketFactoryType()
{
Expand Down Expand Up @@ -932,6 +940,11 @@ protected class TestUrlBuilder : ISlackUrlBuilder
public string Url(string apiMethod, Dictionary<string, object> args) => throw new NotImplementedException();
}

protected class TestLogger : ILogger
{
public void Log(ILogEvent logEvent) => throw new NotImplementedException();
}

protected class TestWebSocketFactory : IWebSocketFactory
{
public IWebSocket Create(string uri) => throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public void UseUrlBuilderFactory()
s => s.GetUrlBuilder());
}

[Test]
public void UseLoggerFactory()
{
UseService<ILogger, TestLogger>(
c => c.UseLogger(r => new TestLogger()),
s => s.GetLogger());
}

[Test]
public void UseWebSocketFactoryFactory()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public void UseUrlBuilderFactory()
s => s.GetUrlBuilder());
}

[Test]
public void UseLoggerFactory()
{
UseService<ILogger, TestLogger>(
c => c.UseLogger(() => new TestLogger()),
s => s.GetLogger());
}

[Test]
public void UseWebSocketFactoryFactory()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using EasyAssertions;
using Newtonsoft.Json;
using NSubstitute;
Expand Down Expand Up @@ -66,6 +65,15 @@ public void UseUrlBuilder()
s => s.GetUrlBuilder());
}

[Test]
public void UseLogger()
{
UseService(
Substitute.For<ILogger>(),
(c, sp) => c.UseLogger(sp),
s => s.GetLogger());
}

[Test]
public void UseWebSocketFactory()
{
Expand Down
Loading

0 comments on commit dc17fb2

Please sign in to comment.