33using FileSyncLibNet . FileSyncJob ;
44using FileSyncLibNet . Logger ;
55using FileSyncLibNet . SyncProviders ;
6+ using Microsoft . Extensions . Logging ;
67using Newtonsoft . Json ;
8+ using Serilog ;
9+ using Serilog . Core ;
710using System ;
811using System . Collections . Generic ;
12+ using System . Diagnostics ;
913using System . IO ;
10- using System . Text . Json . Serialization ;
14+ using System . Linq ;
15+ using System . Net ;
16+ using System . Reflection ;
17+ using System . Runtime . InteropServices ;
18+ using System . Runtime . InteropServices . ComTypes ;
19+ using System . Security . Cryptography . X509Certificates ;
20+ using System . Text ;
21+ using System . Threading ;
22+ using File = System . IO . File ;
1123
1224namespace FileSyncApp
1325{
14- internal class Program
26+ public class Program
1527 {
16- static void Main ( string [ ] args )
28+ public static event EventHandler JobsReady ;
29+ public static volatile bool keepRunning = true ;
30+ public static LoggingLevelSwitch LoggingLevel { get ; private set ; }
31+ public static ILoggerFactory LoggerFactory { get ; private set ; }
32+ public static Dictionary < string , IFileJob > Jobs = new Dictionary < string , IFileJob > ( ) ;
33+ public static void Main ( string [ ] args )
34+ {
35+
36+
37+
38+
39+
40+ LoggingLevel = new LoggingLevelSwitch ( Serilog . Events . LogEventLevel . Information ) ;
41+ #if DEBUG
42+ LoggingLevel = new LoggingLevelSwitch ( Serilog . Events . LogEventLevel . Verbose ) ;
43+ #endif
44+ ConfigureLogger ( ) ;
45+ LoggerFactory = Microsoft . Extensions . Logging . LoggerFactory . Create ( builder => { builder . AddSerilog ( Serilog . Log . Logger ) ; } ) ;
46+ if ( null != args && args . Length > 0 )
47+ {
48+ if ( args . Contains ( "debug" ) )
49+ {
50+ LoggingLevel . MinimumLevel = Serilog . Events . LogEventLevel . Verbose ;
51+ }
52+ }
53+ Console . CancelKeyPress += ( s , e ) => { keepRunning = false ; e . Cancel = true ; } ;
54+ RunProgram ( ) ;
55+
56+ }
57+
58+
59+ static void RunProgram ( )
1760 {
1861 Console . WriteLine ( "FileSyncApp - synchronizing folders and clean them up" ) ;
1962 Dictionary < string , IFileJobOptions > jobOptions = new Dictionary < string , IFileJobOptions > ( ) ;
@@ -34,20 +77,22 @@ static void Main(string[] args)
3477 jobOptions . Add ( "CleanJob" , cleanJob ) ;
3578
3679 var syncFromEdgeToLocal = FileSyncJobOptionsBuilder . CreateBuilder ( )
37- . WithSourcePath ( "\\ \\ 192.168.214.240 \\ share\\ hri \\ production" )
80+ . WithSourcePath ( "\\ \\ edgeip \\ share\\ service \\ production" )
3881 . WithDestinationPath ( "temp" )
3982 . WithFileSyncProvider ( SyncProvider . SMBLib )
4083 . WithSubfolder ( "left" )
4184 . WithSubfolder ( "right" )
4285 . WithCredentials ( new System . Net . NetworkCredential ( "USER" , "Password" , "" ) )
43- . WithInterval ( TimeSpan . FromMinutes ( 10 ) + TimeSpan . FromSeconds ( 25 ) )
86+ . WithInterval ( TimeSpan . FromMinutes ( 10 ) + TimeSpan . FromSeconds ( 25 ) )
4487 . SyncRecursive ( true )
4588 . Build ( ) ;
4689 jobOptions . Add ( "SyncFromEdgeToLocal" , syncFromEdgeToLocal ) ;
47-
90+
91+ var hostname = Dns . GetHostName ( ) ;
92+
4893 var syncFromLocalToRemote = FileSyncJobOptionsBuilder . CreateBuilder ( )
4994 . WithSourcePath ( "temp" )
50- . WithDestinationPath ( "Z: \\ Serienspektren_Import \\ 53600002" )
95+ . WithDestinationPath ( "\\ \\ SERVER \\ Share \\ Subfolder \\ " + hostname )
5196 . WithFileSyncProvider ( SyncProvider . FileIO )
5297 . WithInterval ( TimeSpan . FromMinutes ( 15 ) )
5398 . DeleteAfterBackup ( false ) //sonst werden die Daten wieder neu von der Edge geholt
@@ -59,21 +104,49 @@ static void Main(string[] args)
59104 File . WriteAllText ( "config.json" , json ) ;
60105 }
61106 var readJobOptions = JsonConvert . DeserializeObject < Dictionary < string , IFileJobOptions > > ( File . ReadAllText ( "config.json" ) , jsonSettings ) ;
62- List < IFileJob > Jobs = new List < IFileJob > ( ) ;
63- foreach ( var jobOption in readJobOptions )
107+ //List<IFileJob> Jobs = new List<IFileJob>();
108+
109+ foreach ( var jobOption in readJobOptions )
110+ {
111+
112+ jobOption . Value . Logger = LoggerFactory . CreateLogger ( jobOption . Key ) ;
113+ Jobs . Add ( jobOption . Key , FileSyncJob . CreateJob ( jobOption . Value ) ) ;
114+ }
115+ JobsReady ? . Invoke ( null , EventArgs . Empty ) ;
116+ foreach ( var job in Jobs )
64117 {
65- jobOption . Value . Logger = new StringLogger ( new Action < string > ( ( x ) => { Console . WriteLine ( x ) ; } ) ) ;
66- Jobs . Add ( FileSyncJob . CreateJob ( jobOption . Value ) ) ;
118+ job . Value . StartJob ( ) ;
67119 }
68- foreach ( var job in Jobs )
120+ Console . WriteLine ( "Press Ctrl+C to exit" ) ;
121+ while ( keepRunning )
69122 {
70- job . StartJob ( ) ;
123+ Thread . Sleep ( 200 ) ;
71124 }
72- Console . WriteLine ( "Press Enter to exit" ) ;
73- Console . ReadLine ( ) ;
125+ }
126+
127+
128+ private static void ConfigureLogger ( )
129+ {
130+ string serilogFileTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext:l} {Message:lj}{NewLine}{Exception}" ;
131+ string serilogConsoleTemplate = "{Timestamp:HH:mm:ss.fff}[{Level:u3}]{SourceContext:l} {Message:lj}{NewLine}{Exception}" ;
132+ Serilog . Log . Logger = new LoggerConfiguration ( )
133+ . Enrich . FromLogContext ( )
134+ . WriteTo . Async ( asyncWriteTo => asyncWriteTo . Console ( outputTemplate : serilogConsoleTemplate ) , blockWhenFull : true )
135+ . WriteTo . Async ( asyncWriteTo => asyncWriteTo . File (
136+ ( "FileSyncApp.log" ) ,
137+ retainedFileCountLimit : 10 ,
138+ fileSizeLimitBytes : 1024 * 1024 * 100 ,
139+ rollingInterval : RollingInterval . Day ,
140+ outputTemplate : serilogFileTemplate ) ,
141+ blockWhenFull : true )
142+ . MinimumLevel . ControlledBy ( LoggingLevel )
143+ . CreateLogger ( ) ;
74144
75145
76146 }
147+
148+
149+
77150 }
78151
79152}
0 commit comments