11using System ;
22using System . IO ;
3+ using System . Threading ;
34using JavaToCSharp ;
4- using log4net ;
5- using log4net . Config ;
5+ using Microsoft . Extensions . Logging ;
66
77namespace 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 \t JavaToCSharpCli.exe -f [pathToJavaFile] [pathToCsOutputFile]" ) ;
87- Console . WriteLine ( "Usage: \t JavaToCSharpCli.exe -d [pathToJavaFolder] [pathToCsOutputFolder]" ) ;
95+ Console . WriteLine ( "\t JavaToCSharpCli.exe -d [pathToJavaFolder] [pathToCsOutputFolder]" ) ;
8896 }
8997 }
9098}
0 commit comments