Skip to content

Commit b85cfd6

Browse files
author
Gonzalo Diaz
committed
New Logging system added with LOG_LEVEL environment variable to control behavior.
1 parent 00986cc commit b85cfd6

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<RootNamespace>algorithm_exercises_csharp</RootNamespace>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
9+
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
10+
11+
<!-- Static Analysis -->
12+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
13+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
14+
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
15+
16+
</PropertyGroup>
17+
18+
<!-- Exclude test project from coverage -->
19+
<ItemGroup>
20+
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
25+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
26+
</ItemGroup>
27+
28+
29+
</Project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
namespace algorithm_exercises_csharp;
2+
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
6+
public sealed class LoggerSingleton
7+
{
8+
private static readonly Lazy<LoggerSingleton> _instance = new Lazy<LoggerSingleton>(() => new LoggerSingleton());
9+
10+
public static LoggerSingleton Instance => _instance.Value;
11+
12+
public ILogger Logger { get; }
13+
14+
private LoggerSingleton()
15+
{
16+
// Read the LOG_LEVEL environment variable
17+
var logLevelEnv = Environment.GetEnvironmentVariable("LOG_LEVEL") ?? "Information";
18+
19+
// Convert the environment variable value to LogLevel
20+
if (!Enum.TryParse<LogLevel>(logLevelEnv, ignoreCase: true, out var logLevel))
21+
{
22+
logLevel = LogLevel.Information; // Set the minimum logging level
23+
}
24+
25+
var loggerFactory = LoggerFactory.Create(builder =>
26+
{
27+
builder
28+
.AddConsole()
29+
.SetMinimumLevel(logLevel); // Configura el nivel mínimo de logging
30+
});
31+
32+
Logger = loggerFactory.CreateLogger("GlobalLogger");
33+
34+
Logger.LogInformation("Initializing");
35+
36+
Logger.LogInformation("Info level enabled");
37+
Logger.LogWarning("Warning level enabled");
38+
Logger.LogError("Error level enabled");
39+
Logger.LogDebug("Debug level enabled");
40+
}
41+
}
42+
43+
public static class Log
44+
{
45+
public static ILogger getLogger()
46+
{
47+
return LoggerSingleton.Instance.Logger;
48+
}
49+
50+
public static void info(string message)
51+
{
52+
LoggerSingleton.Instance.Logger.LogInformation(message);
53+
}
54+
55+
public static void warning(string message)
56+
{
57+
LoggerSingleton.Instance.Logger.LogWarning(message);
58+
}
59+
60+
public static void error(string message)
61+
{
62+
LoggerSingleton.Instance.Logger.LogError(message);
63+
}
64+
65+
public static void debug(string message)
66+
{
67+
LoggerSingleton.Instance.Logger.LogDebug(message);
68+
}
69+
}

algorithm-exercises-csharp-test/src/Hello.Test.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ namespace algorithm_exercises_csharp;
33
[TestClass]
44
public class HelloWorldTest
55
{
6+
[TestInitialize]
7+
public void testInitialize()
8+
{
9+
Log.info("Hello World");
10+
}
11+
612
[TestMethod]
713
public void testInstance()
814
{
@@ -34,7 +40,6 @@ public void testHello()
3440
string result = HelloWorld.hello();
3541

3642
Assert.AreEqual(expected, result);
37-
3843
}
3944
}
4045

algorithm-exercises-csharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithm-exercises-csharp-
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithm-exercises-csharp", "algorithm-exercises-csharp\algorithm-exercises-csharp.csproj", "{B162EE62-90C6-4871-B278-390804615987}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithm-exercises-csharp-base", "algorithm-exercises-csharp-base\algorithm-exercises-csharp-base.csproj", "{1BC65C42-83A6-486D-84DB-0DC63002FA24}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{B162EE62-90C6-4871-B278-390804615987}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{B162EE62-90C6-4871-B278-390804615987}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{B162EE62-90C6-4871-B278-390804615987}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

algorithm-exercises-csharp/algorithm-exercises-csharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@
1515

1616
</PropertyGroup>
1717

18+
<ItemGroup>
19+
<ProjectReference Include="../algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj" />
20+
</ItemGroup>
1821
</Project>

0 commit comments

Comments
 (0)