Testing serilog

This commit is contained in:
2023-09-04 12:29:03 +02:00
parent 2b4d10070f
commit 4a8fec1d5f
20 changed files with 324 additions and 19 deletions

Binary file not shown.

BIN
WinFormDi/Local.db-shm Normal file

Binary file not shown.

0
WinFormDi/Local.db-wal Normal file
View File

View File

@ -1,8 +1,11 @@
using DIDemoLib;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using WinFormDiApp.DAL;
using WinFormDiApp.DAL.Data;
using Serilog;
using Serilog.Formatting.Json;
namespace WinFormDiApp
{
@ -14,12 +17,20 @@ namespace WinFormDiApp
[STAThread]
static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(@"D:\logs\winformApp.txt",restrictedToMinimumLevel:Serilog.Events.LogEventLevel.Information,
rollingInterval: RollingInterval.Day)
.CreateLogger();
var host = ContainerConfig.Configure(CreateHostBuilder(args));
using var scope = host.Services.CreateScope();
try
{
Log.Information("Starting of service..");
var services = scope.ServiceProvider;
var context = services.GetRequiredService<ApplicationDbContext>();
@ -34,13 +45,26 @@ namespace WinFormDiApp
}
catch (Exception ex)
{
Console.WriteLine($"An error has occured: {ex.Message}");
Log.Fatal(ex, "Exception in application");
}
finally
{
Log.Information("Exiting service..");
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args);
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureLogging((context, logging) =>
{
logging.ClearProviders();
logging.AddConfiguration(context.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
});

View File

@ -17,6 +17,10 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>

View File

@ -1,16 +1,26 @@
{
"EPPlus": {
"ExcelPackage": {
"LicenseContext": "Noncommercial" //The license context used
}
},
"ConnectionStrings": {
"ConnectionStrings": {
"DatabaseConnection": "Data Source=.\\Local.db"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Information",
"Microsoft*": "Warning"
}
},
"Debug": {
"LogLevel": {
"Default": "Warning",
"Microsoft*": "Warning"
}
},
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"Microsoft*": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -24,6 +24,7 @@ namespace WinFormDiApp
private void frmPayments_Load(object sender, EventArgs e)
{
lvPayments.Items.Clear();
var payments = _accountRecordRepository.GetAllAccounts();
foreach (var account in payments)
{

View File

@ -92,7 +92,7 @@
//
// btnClose
//
btnClose.Location = new Point(758, 418);
btnClose.Location = new Point(758, 506);
btnClose.Name = "btnClose";
btnClose.Size = new Size(75, 23);
btnClose.TabIndex = 1;
@ -107,7 +107,7 @@
lvPayouts.Columns.AddRange(new ColumnHeader[] { columnHeader1, columnHeader2, columnHeader3, columnHeader4, columnHeader5, columnHeader6 });
lvPayouts.Location = new Point(28, 107);
lvPayouts.Name = "lvPayouts";
lvPayouts.Size = new Size(805, 286);
lvPayouts.Size = new Size(805, 393);
lvPayouts.TabIndex = 2;
lvPayouts.UseCompatibleStateImageBehavior = false;
lvPayouts.View = View.Details;
@ -183,7 +183,7 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(856, 453);
ClientSize = new Size(856, 541);
Controls.Add(btnStartRead);
Controls.Add(lblTransFileName);
Controls.Add(btnChooseFile);

View File

@ -17,6 +17,7 @@ namespace WinFormDiApp
_accountRecordRepository = accountRecordRepository;
_readingIn = readingIn;
_logger = logger;
}
@ -42,10 +43,11 @@ namespace WinFormDiApp
if (!_readingIn.ReadAndSaveInvoices(lblTransFileName.Text))
{
var resUlt = _readingIn.readXLS(lblTransFileName.Text);
resUlt.ToList().ForEach(rec => _logger.LogInformation($"Konto :{rec.Konto}, {rec.Belopp}"));
resUlt.ToList().ForEach(rec => _logger.LogInformation("Konto :{sKonto}, {dBelopp}", rec.Konto, rec.Belopp));
}
else
{
lvPayouts.Items.Clear();
var payments = _accountRecordRepository.GetAllAccounts();
foreach (var account in payments)
{
@ -61,5 +63,7 @@ namespace WinFormDiApp
btnStartRead.Enabled = false;
btnStartRead.Visible = false;
}
}
}

View File

@ -120,4 +120,7 @@
<metadata name="ofChooseFile.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root>