Skip to content

Commit

Permalink
Merge pull request #13 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Add Support for Dependency injection
  • Loading branch information
Ali-YousefiTelori authored Oct 19, 2023
2 parents e76ac0d + d37db25 commit e53d939
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>logger wrapper</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>log,logger,logging,log4net</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using EasyMicroservices.Logger.Log4net.Providers;
using EasyMicroservices.Logger.Options;
using log4net;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
///
/// </summary>
public static class Log4netExtensions
{
/// <summary>
///
/// </summary>
/// <param name="options"></param>
/// <param name="log"></param>
/// <returns></returns>
public static LoggerOption UseLog4net(this LoggerOption options, ILog log)
{
options.ThrowIfNull(nameof(options));
log.ThrowIfNull(nameof(log));
LoggerOptionBuilder.UseLogger(() => new Log4netProvider(log));
return options;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<Choose>
<When Condition="$(SolutionFileName.Contains('Net7'))">
<PropertyGroup>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net48;net7.0</TargetFrameworks>
</PropertyGroup>
</Otherwise>
</Choose>

<PropertyGroup>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.1</Version>
<Description>logger wrapper</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>log,logger,logging</PackageTags>
<PackageProjectUrl>https://github.com/EasyMicroservices/Logger</PackageProjectUrl>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DocumentationFile>.\bin\$(Configuration)\$(TargetFramework)\EasyMicroservices.Logger.DependencyInjection.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EasyMicroservices.Logger\EasyMicroservices.Logger.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using EasyMicroservices.Logger.Interfaces;
using EasyMicroservices.Logger.Options;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
///
/// </summary>
public static class LoggerExtensions
{
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection AddLogger(this IServiceCollection services, Action<LoggerOption> options)
{
options.ThrowIfNull(nameof(options));
options(new LoggerOption());
services.AddScoped(service => LoggerOptionBuilder.GetLogger());
return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>logger wrapper</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>log,logger,logging,nlog</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using EasyMicroservices.Logger.NLog.Providers;
using EasyMicroservices.Logger.Options;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
///
/// </summary>
public static class NLogExtensions
{
/// <summary>
///
/// </summary>
/// <param name="options"></param>
/// <param name="logger"></param>
/// <returns></returns>
public static LoggerOption UseLog4net(this LoggerOption options, NLog.Logger logger)
{
options.ThrowIfNull(nameof(options));
logger.ThrowIfNull(nameof(logger));
LoggerOptionBuilder.UseLogger(() => new NLogProvider(logger));
return options;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>logger wrapper</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>log,logger,logging,serilog</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using EasyMicroservices.Logger.Options;
using EasyMicroservices.Logger.Serilog.Providers;
using Serilog;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
///
/// </summary>
public static class SerilogExtensions
{
/// <summary>
///
/// </summary>
/// <param name="options"></param>
/// <param name="loggerConfiguration"></param>
/// <returns></returns>
public static LoggerOption UseLog4net(this LoggerOption options, LoggerConfiguration loggerConfiguration)
{
options.ThrowIfNull(nameof(options));
loggerConfiguration.ThrowIfNull(nameof(loggerConfiguration));
LoggerOptionBuilder.UseLogger(() => new SerilogProvider(loggerConfiguration));
return options;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using EasyMicroservices.Logger.Interfaces;
using System;
using System.Threading.Tasks;
using Xunit;

namespace EasyMicroservices.Logger.Tests
{
public abstract class BaseProviderAsyncTest
{
ILoggerProviderAsync _logger;
public BaseProviderAsyncTest(ILoggerProviderAsync logger)
{
_logger = logger;
}

[Theory]
[InlineData("Hello world Debug")]
[InlineData("Hello world Debug 2")]
public async Task MessageDebug(string message)
{
await _logger.DebugAsync(message);
await _logger.DebugAsync(message, new Exception($"exception {message}"));
}

[Theory]
[InlineData("Hello world Verbose")]
[InlineData("Hello world Verbose 2")]
public async Task MessageVerbose(string message)
{
await _logger.VerboseAsync(message);
await _logger.VerboseAsync(message, new Exception($"exception {message}"));
}

[Theory]
[InlineData("Hello world Information")]
[InlineData("Hello world Information 2")]
public async Task MessageInformation(string message)
{
await _logger.InformationAsync(message);
await _logger.InformationAsync(message, new Exception($"exception {message}"));
}

[Theory]
[InlineData("Hello world Warning")]
[InlineData("Hello world Warning 2")]
public async Task MessageWarning(string message)
{
await _logger.WarningAsync(message);
await _logger.WarningAsync(message, new Exception($"exception {message}"));
}

[Theory]
[InlineData("Hello world Error")]
[InlineData("Hello world Error 2")]
public async Task MessageError(string message)
{
await _logger.ErrorAsync(message);
await _logger.ErrorAsync(message, new Exception($"exception {message}"));
}

[Theory]
[InlineData("Hello world Fatal")]
[InlineData("Hello world Fatal 2")]
public async Task MessageFatal(string message)
{
await _logger.FatalAsync(message);
await _logger.FatalAsync(message, new Exception($"exception {message}"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="EasyMicroservices.Tests" Version="0.0.0.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Xunit.Microsoft.DependencyInjection" Version="7.0.7" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand All @@ -19,6 +21,7 @@

<ItemGroup>
<ProjectReference Include="..\EasyMicroservices.Logger. Log4net\EasyMicroservices.Logger.Log4net.csproj" />
<ProjectReference Include="..\EasyMicroservices.Logger.DependencyInjection\EasyMicroservices.Logger.DependencyInjection.csproj" />
<ProjectReference Include="..\EasyMicroservices.Logger.NLog\EasyMicroservices.Logger.NLog.csproj" />
<ProjectReference Include="..\EasyMicroservices.Logger.Serilog\EasyMicroservices.Logger.Serilog.csproj" />
<ProjectReference Include="..\EasyMicroservices.Logger\EasyMicroservices.Logger.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public Log4netProviderTest() : base(new Log4netProvider(LogManager.GetLogger(typ
{
}
}
public class Log4netProviderAsyncTest : BaseProviderAsyncTest
{
public Log4netProviderAsyncTest() : base(new Log4netProvider(LogManager.GetLogger(typeof(BaseProviderTest))))
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public NLogProviderTest() : base(new NLogProvider(LogManager.GetCurrentClassLogg
{
}
}
public class NLogProviderAsyncTest : BaseProviderAsyncTest
{
public NLogProviderAsyncTest() : base(new NLogProvider(LogManager.GetCurrentClassLogger()))
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ public SerilogProviderTest() : base(new SerilogProvider(new LoggerConfiguration(
{
}
}

public class SerilogProviderAsyncTest : BaseProviderAsyncTest
{
public SerilogProviderAsyncTest() : base(new SerilogProvider(new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("serilog.txt")))
{
}
}
}
13 changes: 10 additions & 3 deletions src/CSharp/EasyMicroservices.Logger/EasyMicroservices.Logger.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Implementations", "Implemen
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{F0D8AE1A-0B1B-4B99-89FB-41BC66943B32}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyMicroservices.Logger.Serilog", "EasyMicroservices.Logger.Serilog\EasyMicroservices.Logger.Serilog.csproj", "{BCE7F22D-6E74-4F9B-BF18-81E0D537CA49}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyMicroservices.Logger.Serilog", "EasyMicroservices.Logger.Serilog\EasyMicroservices.Logger.Serilog.csproj", "{BCE7F22D-6E74-4F9B-BF18-81E0D537CA49}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyMicroservices.Logger.Log4net", "EasyMicroservices.Logger. Log4net\EasyMicroservices.Logger.Log4net.csproj", "{F792825F-75B4-4A07-9091-B9D8B4698A6A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyMicroservices.Logger.Log4net", "EasyMicroservices.Logger. Log4net\EasyMicroservices.Logger.Log4net.csproj", "{F792825F-75B4-4A07-9091-B9D8B4698A6A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyMicroservices.Logger.NLog", "EasyMicroservices.Logger.NLog\EasyMicroservices.Logger.NLog.csproj", "{82A0765F-09D8-4EC2-8A26-C5375C151C83}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyMicroservices.Logger.NLog", "EasyMicroservices.Logger.NLog\EasyMicroservices.Logger.NLog.csproj", "{82A0765F-09D8-4EC2-8A26-C5375C151C83}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyMicroservices.Logger.DependencyInjection", "EasyMicroservices.Logger.DependencyInjection\EasyMicroservices.Logger.DependencyInjection.csproj", "{B06FB19E-791E-43E7-BE00-3EC01698A1AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -47,6 +49,10 @@ Global
{82A0765F-09D8-4EC2-8A26-C5375C151C83}.Debug|Any CPU.Build.0 = Debug|Any CPU
{82A0765F-09D8-4EC2-8A26-C5375C151C83}.Release|Any CPU.ActiveCfg = Release|Any CPU
{82A0765F-09D8-4EC2-8A26-C5375C151C83}.Release|Any CPU.Build.0 = Release|Any CPU
{B06FB19E-791E-43E7-BE00-3EC01698A1AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B06FB19E-791E-43E7-BE00-3EC01698A1AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B06FB19E-791E-43E7-BE00-3EC01698A1AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B06FB19E-791E-43E7-BE00-3EC01698A1AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -60,6 +66,7 @@ Global
{BCE7F22D-6E74-4F9B-BF18-81E0D537CA49} = {B41249C7-F55F-4890-A2A7-177DEA51114C}
{F792825F-75B4-4A07-9091-B9D8B4698A6A} = {B41249C7-F55F-4890-A2A7-177DEA51114C}
{82A0765F-09D8-4EC2-8A26-C5375C151C83} = {B41249C7-F55F-4890-A2A7-177DEA51114C}
{B06FB19E-791E-43E7-BE00-3EC01698A1AE} = {B41249C7-F55F-4890-A2A7-177DEA51114C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D85582B4-E6F8-41D7-9BEC-F4C8B64DDF1B}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>Logger and Auditing system</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>logging,auditing</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace EasyMicroservices.Logger.Interfaces
/// <summary>
/// General logger define here
/// </summary>
public interface ILoggerProviderAsync
public interface ILoggerProviderAsync : ILoggerProvider
{
/// <summary>
/// Verbose level log
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace EasyMicroservices.Logger.Options
{
/// <summary>
///
/// </summary>
public class LoggerOption
{
}
}
Loading

0 comments on commit e53d939

Please sign in to comment.