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

1465 remove warning #809

Merged
merged 14 commits into from
Jun 30, 2020
3 changes: 3 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ skip_branch_with_pr: true
pull_requests:
do_not_increment_build_number: true

nuget:
disable_publish_on_pr: false

skip_tags: true

skip_commits:
Expand Down
12 changes: 12 additions & 0 deletions src/OSPSuite.Core/Extensions/FunctionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace OSPSuite.Core.Extensions
{
public static class FunctionExtensions
{
public static T Compose<T>(this Func<T, T> f1, Func<T, T> f2, T value)
{
return f2.Invoke(f1.Invoke(value));
}
}
}
3 changes: 2 additions & 1 deletion src/OSPSuite.Core/OSPSuite.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
<ItemGroup>
<PackageReference Include="csmpfit" Version="1.1.1" />
<PackageReference Include="MathNet.Numerics" Version="4.9.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.5" />
<PackageReference Include="OSPSuite.Serializer" Version="3.0.0.1" />
<PackageReference Include="OSPSuite.FuncParser" Version="4.0.0.50" GeneratePathProperty="true" />
<PackageReference Include="OSPSuite.SimModel" Version="4.0.0.42" GeneratePathProperty="true" />
Expand Down
14 changes: 14 additions & 0 deletions src/OSPSuite.Core/Services/ILoggerCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace OSPSuite.Core.Services
{
public interface ILoggerCreator
{
ILogger GetOrCreateLogger(string categoryName);

ILoggerCreator AddLoggingBuilderConfiguration(Func<ILoggingBuilder, ILoggingBuilder> configuration);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Microsoft.Extensions.Logging;
using System;

namespace OSPSuite.Core.Services
{
public interface ILogger
public interface IOSPLogger
{
/// <summary>
/// Logs the <paramref name="message" /> using the provided <paramref name="logLevel" /> for the
Expand Down
28 changes: 14 additions & 14 deletions src/OSPSuite.Core/Services/LoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ namespace OSPSuite.Core.Services
{
public static class LoggerExtensions
{
public static void AddError(this ILogger logger, string message, string categoryName = null) => addToLog(logger, message, LogLevel.Error, categoryName);
public static void AddError(this IOSPLogger logger, string message, string categoryName = null) => addToLog(logger, message, LogLevel.Error, categoryName);

public static void AddError<T>(this ILogger logger, string message) => addToLog<T>(logger, message, LogLevel.Error);
public static void AddError<T>(this IOSPLogger logger, string message) => addToLog<T>(logger, message, LogLevel.Error);

public static void AddCriticalError(this ILogger logger, string message, string categoryName = null) => logger.AddToLog(message, LogLevel.Critical, categoryName);
public static void AddCriticalError(this IOSPLogger logger, string message, string categoryName = null) => logger.AddToLog(message, LogLevel.Critical, categoryName);

public static void AddCriticalError<T>(this ILogger logger, string message) => addToLog<T>(logger, message, LogLevel.Critical);
public static void AddCriticalError<T>(this IOSPLogger logger, string message) => addToLog<T>(logger, message, LogLevel.Critical);

public static void AddInfo(this ILogger logger, string message, string categoryName = null) => logger.AddToLog(message, LogLevel.Information, categoryName);
public static void AddInfo(this IOSPLogger logger, string message, string categoryName = null) => logger.AddToLog(message, LogLevel.Information, categoryName);

public static void AddInfo<T>(this ILogger logger, string message) => addToLog<T>(logger, message, LogLevel.Information);
public static void AddInfo<T>(this IOSPLogger logger, string message) => addToLog<T>(logger, message, LogLevel.Information);

public static void AddWarning(this ILogger logger, string message, string categoryName = null) => logger.AddToLog(message, LogLevel.Warning, categoryName);
public static void AddWarning(this IOSPLogger logger, string message, string categoryName = null) => logger.AddToLog(message, LogLevel.Warning, categoryName);

public static void AddWarning<T>(this ILogger logger, string message) => addToLog<T>(logger, message, LogLevel.Warning);
public static void AddWarning<T>(this IOSPLogger logger, string message) => addToLog<T>(logger, message, LogLevel.Warning);

public static void AddDebug(this ILogger logger, string message, string categoryName = null) => logger.AddToLog(message, LogLevel.Debug, categoryName);
public static void AddDebug(this IOSPLogger logger, string message, string categoryName = null) => logger.AddToLog(message, LogLevel.Debug, categoryName);

public static void AddDebug<T>(this ILogger logger, string message) => addToLog<T>(logger, message, LogLevel.Debug);
public static void AddDebug<T>(this IOSPLogger logger, string message) => addToLog<T>(logger, message, LogLevel.Debug);

public static void AddException(this ILogger logger, Exception exception, string categoryName = null)
public static void AddException(this IOSPLogger logger, Exception exception, string categoryName = null)
{
//Info message only => Should be shown as warning in log
if (exception.IsInfoException())
Expand All @@ -39,14 +39,14 @@ public static void AddException(this ILogger logger, Exception exception, string
logger.AddError(exception.ExceptionMessageWithStackTrace(false), categoryName);
}

public static void AddException<T>(this ILogger logger, Exception exception) => logger.AddException(exception, typeof(T).Name);
public static void AddException<T>(this IOSPLogger logger, Exception exception) => logger.AddException(exception, typeof(T).Name);

private static void addToLog(ILogger logger, string message, LogLevel logLevel, string category)
private static void addToLog(IOSPLogger logger, string message, LogLevel logLevel, string category)
{
logger.AddToLog(message, logLevel, category);
}

private static void addToLog<T>(ILogger logger, string message, LogLevel logLevel)
private static void addToLog<T>(IOSPLogger logger, string message, LogLevel logLevel)
{
logger.AddToLog(message, logLevel, typeof(T).Name);
}
Expand Down
3 changes: 1 addition & 2 deletions src/OSPSuite.Infrastructure.Import/Services/ImportLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using OSPSuite.Core.Services;
using OSPSuite.Utility.Exceptions;
using OSPSuite.Utility.Extensions;
using ILogger = OSPSuite.Core.Services.ILogger;

namespace OSPSuite.Infrastructure.Import.Services
{
public interface IImportLogger : ILogger
public interface IImportLogger : IOSPLogger
{
IEnumerable<LogEntry> Entries { get; }
IEnumerable<string> Log { get; }
Expand Down
10 changes: 10 additions & 0 deletions src/OSPSuite.Infrastructure/InfrastructureRegister.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OSPSuite.Core;
using OSPSuite.Core.Services;
using OSPSuite.Infrastructure.Services;
using OSPSuite.Utility.Compression;
using OSPSuite.Utility.Container;
Expand All @@ -13,9 +14,18 @@ public override void RegisterInContainer(IContainer container)
{
scan.AssemblyContainingType<InfrastructureRegister>();
scan.WithConvention(new OSPSuiteRegistrationConvention(registerConcreteType: true));
scan.ExcludeType<OSPLogger>();
});

registerThirdPartyComponents(container);
registerLogging(container);
}

private static void registerLogging(IContainer container)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. Well done

{
var loggerCreator = new LoggerCreator();
container.RegisterImplementationOf((ILoggerCreator)loggerCreator);
container.Register<IOSPLogger, OSPLogger>(LifeStyle.Singleton);
}

private static void registerThirdPartyComponents(IContainer container)
Expand Down
52 changes: 0 additions & 52 deletions src/OSPSuite.Infrastructure/Logging/FileLogger.cs

This file was deleted.

35 changes: 0 additions & 35 deletions src/OSPSuite.Infrastructure/Logging/FileLoggerExtensions.cs

This file was deleted.

45 changes: 0 additions & 45 deletions src/OSPSuite.Infrastructure/Logging/FileLoggerProvider.cs

This file was deleted.

2 changes: 2 additions & 0 deletions src/OSPSuite.Infrastructure/OSPSuite.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="OSPSuite.Utility" Version="4.0.0.4" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
<PackageReference Include="SharpZipLib" Version="1.2.0" />
</ItemGroup>

Expand Down
44 changes: 44 additions & 0 deletions src/OSPSuite.Infrastructure/Services/LoggerCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.Extensions.Logging;
using OSPSuite.Core.Extensions;
using OSPSuite.Core.Services;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace OSPSuite.Infrastructure.Services
{
public class LoggerCreator : ILoggerCreator
{
private readonly ConcurrentDictionary<string, ILogger> _loggerDict = new ConcurrentDictionary<string, ILogger>();
private List<Func<ILoggingBuilder, ILoggingBuilder>> _loggingBuilderConfigurations = new List<Func<ILoggingBuilder, ILoggingBuilder>>() { builder => builder };

public ILoggerCreator AddLoggingBuilderConfiguration(Func<ILoggingBuilder, ILoggingBuilder> configuration)
{
_loggingBuilderConfigurations.Add(configuration);
return this;
}

public ILogger GetOrCreateLogger(string categoryName)
{
var logger = _loggerDict.GetOrAdd(categoryName, (_) => SetupLogger(categoryName));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small improvement. Return directly instead of creating a variable

return logger;
}

private ILogger SetupLogger(string categoryName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not follow our convention. Private method => camelCase => setupLogger

{
ILogger logger;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small improvement.
REturn directlu in the using instead of creating a variable outside and returning it


using (var loggerFactory = LoggerFactory.Create(
builder =>
_loggingBuilderConfigurations.Aggregate(
(f1, f2) => config => f1.Compose(f2, config)
).Invoke(builder)
))
{
logger = loggerFactory.CreateLogger(categoryName);
};
return logger;
}
}
}
19 changes: 19 additions & 0 deletions src/OSPSuite.Infrastructure/Services/LoggingBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

using Microsoft.Extensions.Logging;
using Serilog;

namespace OSPSuite.Infrastructure.Services
{
public static class LoggingBuilderExtensions
{
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string logFileFullPath)
{
builder.AddSerilog(
new LoggerConfiguration()
.WriteTo.File(logFileFullPath)
.CreateLogger()
);
return builder;
}
}
}
Loading