Skip to content

Commit d7b99e4

Browse files
Merge pull request #11 from andreaskueffel/development
Add RememberLastSync property to improve performance
2 parents 55db313 + 17dff11 commit d7b99e4

17 files changed

Lines changed: 717 additions & 47 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,4 @@ FodyWeavers.xsd
398398
*.sln.iml
399399
/*.sln
400400
/FileSyncLibNet/FileSyncLibNet.sln
401+
/FileSyncAppWin/pub

FileSyncApp/FileSyncApp.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -7,6 +7,12 @@
77

88
<ItemGroup>
99
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
10+
<PackageReference Include="Serilog" Version="3.0.1" />
11+
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
12+
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
13+
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
14+
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
15+
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
1016
</ItemGroup>
1117

1218
<ItemGroup>

FileSyncApp/Program.cs

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,60 @@
33
using FileSyncLibNet.FileSyncJob;
44
using FileSyncLibNet.Logger;
55
using FileSyncLibNet.SyncProviders;
6+
using Microsoft.Extensions.Logging;
67
using Newtonsoft.Json;
8+
using Serilog;
9+
using Serilog.Core;
710
using System;
811
using System.Collections.Generic;
12+
using System.Diagnostics;
913
using 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

1224
namespace 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
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net6.0-windows</TargetFramework>
6+
7+
<UseWindowsForms>true</UseWindowsForms>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\FileSyncApp\FileSyncApp.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

FileSyncAppWin/MainForm.Designer.cs

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FileSyncAppWin/MainForm.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using FileSyncLibNet.Commons;
2+
3+
namespace FileSyncAppWin
4+
{
5+
public partial class MainForm : Form
6+
{
7+
Thread consoleThread;
8+
9+
public MainForm()
10+
{
11+
InitializeComponent();
12+
this.FormClosing += (s, e) => { FileSyncApp.Program.keepRunning = false; consoleThread?.Join(10_000); };
13+
consoleThread = new Thread(() =>
14+
{
15+
16+
FileSyncApp.Program.Main(null);
17+
});
18+
FileSyncApp.Program.JobsReady += (s,e)=> {
19+
foreach(var job in FileSyncApp.Program.Jobs)
20+
{
21+
job.Value.JobStarted += (j, text) =>
22+
{
23+
NewLogOutput($"{job.Key} STARTED - {text.Status} {text.Exception}");
24+
};
25+
job.Value.JobFinished += (j, text) =>
26+
{
27+
NewLogOutput($"{job.Key} FINISHED - {text.Status} {text.Exception}");
28+
};
29+
job.Value.JobError += (j, text) =>
30+
{
31+
NewLogOutput($"{job.Key} ERROR - {text.Status} {text.Exception}");
32+
};
33+
34+
}
35+
36+
37+
};
38+
consoleThread.Start();
39+
this.Resize += ((s, e) =>
40+
{
41+
this.SuspendLayout();
42+
this.BeginInvoke(() =>
43+
{
44+
if (WindowState == FormWindowState.Minimized && ShowInTaskbar)
45+
{
46+
ShowInTaskbar = false;
47+
notifyIcon1.ShowBalloonTip(10_000, "FileSyncAppWin", "I am here", ToolTipIcon.Info);
48+
notifyIcon1.Visible = true;
49+
}
50+
});
51+
52+
53+
this.ResumeLayout();
54+
});
55+
notifyIcon1.Click += (s, e) =>
56+
{
57+
this.BeginInvoke(() => { WindowState = FormWindowState.Normal; ShowInTaskbar = true; });
58+
};
59+
notifyIcon1.BalloonTipClicked += (s, e) => { this.BeginInvoke(() => { WindowState = FormWindowState.Normal; ShowInTaskbar = true; }); };
60+
//notifyIcon1.BalloonTipShown += (s, e) => { ShowInTaskbar = false; };
61+
62+
//this.WindowState = FormWindowState.Minimized;
63+
}
64+
65+
66+
67+
68+
private void NewLogOutput(string e)
69+
{
70+
71+
this.BeginInvoke(() => { textBox1.Text = string.Join(Environment.NewLine, (new string[] { $"{DateTime.Now.ToString("HH:mm:ss.fff")} {e}" }).Concat(textBox1.Text.Split(Environment.NewLine).Take(1000))); });
72+
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)