Skip to content

Commit 43eb07b

Browse files
committed
Replace log4net with NLog for improved logging functionality
1 parent e26d183 commit 43eb07b

4 files changed

Lines changed: 19 additions & 68 deletions

File tree

MyNumberNET_CLI/MyNumberNET_CLI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
<LangVersion>latest</LangVersion>
1919
</PropertyGroup>
2020
<ItemGroup>
21-
<None Update="log4net.config">
21+
<None Update="nlog.config">
2222
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2323
</None>
2424
</ItemGroup>
2525
<ItemGroup>
26-
<PackageReference Include="log4net" Version="2.0.10" />
26+
<PackageReference Include="NLog" Version="5.2.0" />
2727
<PackageReference Include="System.Console" Version="4.3.1" />
2828
<PackageReference Include="System.Net.Sockets" Version="4.3.0" />
2929
</ItemGroup>

MyNumberNET_CLI/Program.cs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,25 @@
44
using System.Reflection;
55
using System.Text.RegularExpressions;
66
using System.Xml;
7-
using log4net;
8-
using log4net.Config;
9-
using log4net.Repository.Hierarchy;
7+
using NLog;
108
using MyNumberNET;
119
using static System.Char;
1210

1311
namespace MyNumberNET_CLI
1412
{
1513
public class Program
1614
{
17-
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
15+
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
1816

1917
public static int Main(string[] args)
2018
{
2119
try
2220
{
23-
var result = InitializeLogging();
24-
if (!result)
25-
throw new Exception("Logging could not be enabled.");
21+
// NLog automatically loads nlog.config, no manual initialization needed
2622
}
2723
catch (Exception ex)
2824
{
29-
Log.Fatal(ex.Message);
25+
Log.Fatal(ex, ex.Message);
3026
return -1;
3127
}
3228

@@ -183,8 +179,7 @@ public static int Main(string[] args)
183179
}
184180
catch (Exception ex)
185181
{
186-
Log.Fatal(ex.Message);
187-
Log.Debug(ex.StackTrace);
182+
Log.Fatal(ex, ex.Message);
188183
return -1;
189184
}
190185
}
@@ -379,32 +374,6 @@ private static int[] Increment(int[] input)
379374
return input;
380375
}
381376

382-
/// <summary>
383-
/// Initialize logging
384-
/// </summary>
385-
private static bool InitializeLogging()
386-
{
387-
try
388-
{
389-
// Configuration for logging
390-
var log4NetConfig = new XmlDocument();
391-
392-
using (var reader = new StreamReader(new FileStream("log4net.config", FileMode.Open, FileAccess.Read)))
393-
{
394-
log4NetConfig.Load(reader);
395-
}
396-
397-
var rep = LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(Hierarchy));
398-
XmlConfigurator.Configure(rep, log4NetConfig["log4net"]);
399-
return true;
400-
}
401-
catch (Exception ex)
402-
{
403-
Console.WriteLine("Error initializing the logging.");
404-
Console.WriteLine(ex.Message);
405-
return false;
406-
}
407-
}
408377

409378
private enum RangeMode
410379
{

MyNumberNET_CLI/log4net.config

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

MyNumberNET_CLI/nlog.config

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<targets>
5+
<target xsi:type="File" name="logfile" fileName="${basedir}/log.txt" layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
6+
<target xsi:type="Console" name="logconsole" layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
7+
</targets>
8+
<rules>
9+
<logger name="*" minlevel="Info" writeTo="logconsole,logfile" />
10+
</rules>
11+
</nlog>
12+

0 commit comments

Comments
 (0)