Skip to content

Commit 89ac903

Browse files
committed
Added built-in Logger class based on MEL. Removed NLog package.
1 parent da48abf commit 89ac903

File tree

9 files changed

+297
-34
lines changed

9 files changed

+297
-34
lines changed

MIG/Gateways/WebSocketGateway.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This file is part of MIG (https://github.com/genielabs/mig-service-dotnet)
33
4-
Copyright (2012-2023) G-Labs (https://github.com/genielabs)
4+
Copyright (2012-2025) G-Labs (https://github.com/genielabs)
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

MIG/Logging/LogManager.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
This file is part of MIG (https://github.com/genielabs/mig-service-dotnet)
3+
4+
Copyright (2012-2025) G-Labs (https://github.com/genielabs)
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
/*
20+
* Author: Generoso Martello <gene@homegenie.it>
21+
* Project Homepage: https://github.com/genielabs/mig-service-dotnet
22+
*/
23+
24+
using Microsoft.Extensions.Logging;
25+
using System.IO;
26+
using System.Runtime.CompilerServices;
27+
using Microsoft.Extensions.DependencyInjection;
28+
29+
namespace MIG.Logging
30+
{
31+
public static class LogManager
32+
{
33+
private static ILoggerFactory _factory;
34+
private static readonly object _lock = new object();
35+
36+
/// <summary>
37+
/// Initializes the LogManager with the application's ILoggerFactory.
38+
/// This is the recommended approach for production applications.
39+
/// </summary>
40+
public static void Initialize(ILoggerFactory loggerFactory)
41+
{
42+
lock (_lock)
43+
{
44+
_factory = loggerFactory;
45+
}
46+
}
47+
48+
/// <summary>
49+
/// Gets a logger for the specified category name.
50+
/// </summary>
51+
public static Logger GetLogger(string categoryName)
52+
{
53+
if (_factory == null)
54+
{
55+
lock (_lock)
56+
{
57+
if (_factory == null)
58+
{
59+
#if NET6_0_OR_GREATER
60+
_factory = LoggerFactory.Create(builder =>
61+
{
62+
builder
63+
.AddSimpleConsole(options =>
64+
{
65+
options.SingleLine = true;
66+
options.TimestampFormat = "HH:mm:ss ";
67+
})
68+
.SetMinimumLevel(LogLevel.Trace);
69+
});
70+
#else
71+
var services = new ServiceCollection();
72+
services.AddLogging(builder =>
73+
{
74+
builder.AddConsole();
75+
builder.SetMinimumLevel(LogLevel.Trace);
76+
});
77+
var serviceProvider = services.BuildServiceProvider();
78+
_factory = serviceProvider.GetService<ILoggerFactory>();
79+
#endif
80+
81+
var initLogger = _factory.CreateLogger("MIG.LogManager");
82+
initLogger.LogWarning("LogManager was not initialized. A default console logger will be used. For production applications, call LogManager.Initialize(loggerFactory) at startup.");
83+
}
84+
}
85+
}
86+
return new Logger(_factory.CreateLogger(categoryName));
87+
}
88+
89+
/// <summary>
90+
/// Gets a logger using the caller's file name as the category.
91+
/// </summary>
92+
public static Logger GetCurrentClassLogger([CallerFilePath] string callerFilePath = "")
93+
{
94+
var categoryName = Path.GetFileNameWithoutExtension(callerFilePath);
95+
return GetLogger(categoryName);
96+
}
97+
}
98+
}

MIG/Logging/Logger.cs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
This file is part of MIG (https://github.com/genielabs/mig-service-dotnet)
3+
4+
Copyright (2012-2025) G-Labs (https://github.com/genielabs)
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
/*
20+
* Author: Generoso Martello <gene@homegenie.it>
21+
* Project Homepage: https://github.com/genielabs/mig-service-dotnet
22+
*/
23+
24+
using Microsoft.Extensions.Logging;
25+
using System;
26+
27+
namespace MIG.Logging
28+
{
29+
/// <summary>
30+
/// A wrapper class that mimics the NLog Logger API but uses a Microsoft.Extensions.Logging.ILogger internally.
31+
/// This provides backward compatibility for existing code.
32+
/// </summary>
33+
public class Logger
34+
{
35+
private readonly ILogger _melLogger;
36+
37+
internal Logger(ILogger melLogger)
38+
{
39+
_melLogger = melLogger ?? throw new ArgumentNullException(nameof(melLogger));
40+
}
41+
42+
public void Debug(string message, params object[] args)
43+
{
44+
_melLogger.LogDebug(message, args);
45+
}
46+
47+
public void Info(string message, params object[] args)
48+
{
49+
_melLogger.LogInformation(message, args);
50+
}
51+
52+
public void Warn(string message, params object[] args)
53+
{
54+
_melLogger.LogWarning(message, args);
55+
}
56+
57+
public void Error(string message, params object[] args)
58+
{
59+
_melLogger.LogError(message, args);
60+
}
61+
62+
public void Error(Exception exception, string message = null, params object[] args)
63+
{
64+
if (string.IsNullOrEmpty(message))
65+
{
66+
message = exception.Message;
67+
}
68+
_melLogger.LogError(exception, message, args);
69+
}
70+
71+
public void Error(Exception exception)
72+
{
73+
_melLogger.LogError(exception, exception.Message);
74+
}
75+
76+
public void Trace(string message, params object[] args)
77+
{
78+
_melLogger.LogTrace(message, args);
79+
}
80+
81+
82+
public void Debug(object value)
83+
{
84+
_melLogger.LogDebug("{ObjectValue}", value);
85+
}
86+
87+
public void Info(object value)
88+
{
89+
_melLogger.LogInformation("{ObjectValue}", value);
90+
}
91+
92+
public void Warn(object value)
93+
{
94+
_melLogger.LogWarning("{ObjectValue}", value);
95+
}
96+
97+
public void Error(object value)
98+
{
99+
if (value is Exception ex)
100+
{
101+
Error(ex);
102+
}
103+
else
104+
{
105+
_melLogger.LogError("{ObjectValue}", value);
106+
}
107+
}
108+
109+
public void Trace(object value)
110+
{
111+
_melLogger.LogTrace("{ObjectValue}", value);
112+
}
113+
}
114+
}

MIG/MIG.csproj

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<PackageId>MIG</PackageId>
4-
<Version>1.1.0</Version>
4+
<Version>1.3.0</Version>
55
<Authors>Generoso Martello</Authors>
66
<Company>G-Labs</Company>
77
<Description>MIG is a .NET library providing an integrated solution for developing multi-protocol networked applications and real time web applications.</Description>
88
<PackageLicense>./LICENSE</PackageLicense>
99
<PackageProjectUrl>https://github.com/genielabs/mig-service-dotnet/</PackageProjectUrl>
1010
<PackageTags>web gateway middleware multi-protocol networking interoperability</PackageTags>
11-
<RuntimeIdentifiers>win-arm64;linux-arm;linux-arm64;win-x64;osx-x64;linux-x64</RuntimeIdentifiers>
1211
<OutputType>Library</OutputType>
1312
<TargetFrameworks>netstandard2.0;net472;net6.0;net8.0;net9.0</TargetFrameworks>
1413
</PropertyGroup>
@@ -19,23 +18,50 @@
1918
</None>
2019
</ItemGroup>
2120

22-
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
23-
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1" />
21+
<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
22+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.9"/>
23+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.9"/>
24+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.9"/>
25+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.9"/>
26+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.9"/>
27+
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.9"/>
2428
</ItemGroup>
25-
29+
30+
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
31+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0"/>
32+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.1"/>
33+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1"/>
34+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1"/>
35+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1"/>
36+
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0"/>
37+
</ItemGroup>
38+
2639
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
27-
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.1" />
40+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.2"/>
41+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.1"/>
42+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.1"/>
43+
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.1"/>
44+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.1"/>
45+
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.1"/>
2846
</ItemGroup>
2947

30-
<ItemGroup>
31-
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.9" />
32-
<PackageReference Include="WebsocketSharp.Standard2" Version="2022.4.16.1520" />
33-
<PackageReference Include="MessagePack" Version="3.1.4" />
34-
<PackageReference Include="MessagePack.Annotations" Version="3.1.4" />
35-
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
36-
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
37-
<PackageReference Include="NLog.Extensions.Logging" Version="6.0.4" />
38-
<PackageReference Include="Ude.NetStandard" Version="1.2.0" />
48+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net472' ">
49+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0"/>
50+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0"/>
51+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0"/>
52+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0"/>
53+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0"/>
54+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0"/>
55+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0"/>
56+
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1"/>
3957
</ItemGroup>
4058

59+
<ItemGroup>
60+
<PackageReference Include="Microsoft.CSharp" Version="4.7.0"/>
61+
<PackageReference Include="WebsocketSharp.Standard2" Version="2022.4.16.1520"/>
62+
<PackageReference Include="MessagePack" Version="3.1.4"/>
63+
<PackageReference Include="MessagePack.Annotations" Version="3.1.4"/>
64+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4"/>
65+
<PackageReference Include="Ude.NetStandard" Version="1.2.0"/>
66+
</ItemGroup>
4167
</Project>

MIG/MigService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This file is part of MIG (https://github.com/genielabs/mig-service-dotnet)
33
4-
Copyright (2012-2023) G-Labs (https://github.com/genielabs)
4+
Copyright (2012-2025) G-Labs (https://github.com/genielabs)
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -25,10 +25,10 @@ limitations under the License.
2525
using System.Collections.Generic;
2626
using System.IO;
2727
using System.Linq;
28-
using NLog;
2928
using System.Reflection;
3029
using MessagePack;
3130
using MIG.Config;
31+
using MIG.Logging;
3232
using Gateway = MIG.Config.Gateway;
3333

3434
namespace MIG

Test.WebService/NLog.config

Lines changed: 0 additions & 12 deletions
This file was deleted.

Test.WebService/Program.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This file is part of MIG (https://github.com/genielabs/mig-service-dotnet)
33
4-
Copyright (2012-2023) G-Labs (https://github.com/genielabs)
4+
Copyright (2012-2025) G-Labs (https://github.com/genielabs)
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -16,9 +16,14 @@ You may obtain a copy of the License at
1616
limitations under the License.
1717
*/
1818

19+
using Microsoft.Extensions.Configuration;
20+
using Microsoft.Extensions.DependencyInjection;
21+
using Microsoft.Extensions.Logging;
22+
1923
using MIG;
2024
using MIG.Gateways;
2125
using MIG.Gateways.Authentication;
26+
using MIG.Logging;
2227

2328
namespace Test.WebService
2429
{
@@ -34,7 +39,23 @@ class MainClass
3439
{
3540
public static void Main(string[] args)
3641
{
37-
var Log = MigService.Log;
42+
var configuration = new ConfigurationBuilder()
43+
.SetBasePath(Directory.GetCurrentDirectory())
44+
.AddJsonFile("appsettings.json", optional: false)
45+
.Build();
46+
47+
var serviceProvider = new ServiceCollection()
48+
.AddLogging(builder =>
49+
{
50+
builder.AddConfiguration(configuration.GetSection("Logging"));
51+
builder.AddConsole();
52+
})
53+
.BuildServiceProvider();
54+
55+
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
56+
LogManager.Initialize(loggerFactory);
57+
58+
var Log = LogManager.GetLogger("Test.WebService");
3859
string webServicePort = "8088";
3960
string webSocketPort = "8181";
4061

Test.WebService/Test.WebService.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
<None Include="app\index.html">
1919
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2020
</None>
21-
<None Include="NLog.config">
22-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
<None Update="appsettings.json">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2323
</None>
2424
</ItemGroup>
2525

0 commit comments

Comments
 (0)