Skip to content

Commit 8d1a005

Browse files
committed
Change log4net to use Microsoft.Extensions.Logging.
1 parent 25ed205 commit 8d1a005

File tree

3 files changed

+25
-106
lines changed

3 files changed

+25
-106
lines changed
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Version>2.0.2</Version>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net5.0</TargetFramework>
66
</PropertyGroup>
77
<ItemGroup>
8-
<PackageReference Include="log4net" Version="2.0.12" />
8+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
99
</ItemGroup>
1010
<ItemGroup>
1111
<ProjectReference Include="..\JavaToCSharp\JavaToCSharp.csproj" />
1212
</ItemGroup>
13-
<ItemGroup>
14-
<None Update="log4net.config">
15-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16-
</None>
17-
</ItemGroup>
1813
</Project>

JavaToCSharpCli/Program.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
using System;
22
using System.IO;
3+
using System.Threading;
34
using JavaToCSharp;
4-
using log4net;
5-
using log4net.Config;
5+
using Microsoft.Extensions.Logging;
66

77
namespace JavaToCSharpCli
88
{
99
public class Program
1010
{
11-
private static readonly ILog _logger = LogManager.GetLogger(typeof(Program));
11+
private static readonly ILogger _logger;
12+
13+
static Program()
14+
{
15+
var loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole().SetMinimumLevel(LogLevel.Information));
16+
_logger = loggerFactory.CreateLogger<Program>();
17+
}
18+
1219
public static void Main(string[] args)
1320
{
14-
GlobalContext.Properties["appname"] = "JavaToCSharpCli";
15-
XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
1621
try
1722
{
1823
if (args == null || args.Length < 3)
@@ -31,10 +36,13 @@ public static void Main(string[] args)
3136
break;
3237
}
3338
}
34-
catch(Exception ex)
39+
catch (Exception ex)
3540
{
36-
_logger.Error(ex.Message, ex);
41+
_logger.LogError(ex.Message, ex);
3742
}
43+
44+
// allow logger background thread to flush
45+
Thread.Sleep(TimeSpan.FromSeconds(1));
3846
}
3947

4048
private static void ConvertToCSharpDir(string folderPath, string outputFolderPath)
@@ -46,17 +54,17 @@ private static void ConvertToCSharpDir(string folderPath, string outputFolderPat
4654
foreach (var f in input.GetFiles("*.java", SearchOption.AllDirectories))
4755
ConvertToCSharpFile(
4856
f.FullName,
49-
Path.Combine(output.FullName, f.Name.Replace(f.Extension, ".cs")),
57+
Path.Combine(output.FullName, f.Name.Replace(f.Extension, ".cs")),
5058
false);
5159
}
5260
else
53-
_logger.Info("Java input folder doesn't exist!");
61+
_logger.LogError("Java input folder {path} doesn't exist!", folderPath);
5462
}
5563

5664
private static void ConvertToCSharpFile(string filePath, string outputFilePath, bool overwrite = true)
5765
{
5866
if (!overwrite && File.Exists(outputFilePath))
59-
_logger.Info($"{outputFilePath} exists, skip to next.");
67+
_logger.LogInformation("{outputFilePath} exists, skip to next.", outputFilePath);
6068
else if (File.Exists(filePath))
6169
{
6270
try
@@ -65,26 +73,26 @@ private static void ConvertToCSharpFile(string filePath, string outputFilePath,
6573
var options = new JavaConversionOptions();
6674

6775
options.WarningEncountered += (sender, eventArgs)
68-
=> _logger.Warn($"[WARNING] Line {eventArgs.JavaLineNumber}: {eventArgs.Message}");
76+
=> _logger.LogWarning("Line {line}: {message}", eventArgs.JavaLineNumber, eventArgs.Message);
6977

7078
var parsed = JavaToCSharpConverter.ConvertText(javaText, options);
7179

7280
File.WriteAllText(outputFilePath, parsed);
73-
_logger.Info($"{filePath} was done!");
81+
_logger.LogInformation("{filePath} converted!", filePath);
7482
}
7583
catch (Exception ex)
7684
{
77-
_logger.Error($"{filePath} was failed! err = " + ex.Message, ex);
85+
_logger.LogError("{filePath} failed! {type}: {message}", filePath, ex.GetType().Name, ex.Message);
7886
}
7987
}
8088
else
81-
_logger.Info("Java input file doesn't exist!");
89+
_logger.LogError("Java input file {filePath} doesn't exist!", filePath);
8290
}
8391

8492
private static void ShowHelp()
8593
{
8694
Console.WriteLine("Usage:\r\n\tJavaToCSharpCli.exe -f [pathToJavaFile] [pathToCsOutputFile]");
87-
Console.WriteLine("Usage:\tJavaToCSharpCli.exe -d [pathToJavaFolder] [pathToCsOutputFolder]");
95+
Console.WriteLine("\tJavaToCSharpCli.exe -d [pathToJavaFolder] [pathToCsOutputFolder]");
8896
}
8997
}
9098
}

JavaToCSharpCli/log4net.config

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

0 commit comments

Comments
 (0)