Browse Source

Make wpf template logger work

pull/7934/head
liangshiwei 5 years ago
parent
commit
92d3e6800b
  1. 43
      templates/wpf/src/MyCompanyName.MyProjectName/App.xaml.cs
  2. 12
      templates/wpf/src/MyCompanyName.MyProjectName/MainWindow.xaml.cs
  3. 2
      templates/wpf/src/MyCompanyName.MyProjectName/MyCompanyName.MyProjectName.csproj

43
templates/wpf/src/MyCompanyName.MyProjectName/App.xaml.cs

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@ -22,19 +17,6 @@ namespace MyCompanyName.MyProjectName
private readonly IAbpApplicationWithExternalServiceProvider _application;
public App()
{
_host = Host
.CreateDefaultBuilder(null)
.UseAutofac()
.UseSerilog()
.ConfigureServices((hostContext, services) =>
{
services.AddApplication<MyProjectNameModule>();
}).Build();
_application = _host.Services.GetService<IAbpApplicationWithExternalServiceProvider>();
}
protected async override void OnStartup(StartupEventArgs e)
{
Log.Logger = new LoggerConfiguration()
#if DEBUG
@ -47,6 +29,12 @@ namespace MyCompanyName.MyProjectName
.WriteTo.Async(c => c.File("Logs/logs.txt"))
.CreateLogger();
_host = CreateHostBuilder();
_application = _host.Services.GetService<IAbpApplicationWithExternalServiceProvider>();
}
protected override async void OnStartup(StartupEventArgs e)
{
try
{
Log.Information("Starting WPF host.");
@ -60,22 +48,31 @@ namespace MyCompanyName.MyProjectName
{
Log.Fatal(ex, "Host terminated unexpectedly!");
}
finally
{
Log.CloseAndFlush();
}
}
protected async override void OnExit(ExitEventArgs e)
protected override async void OnExit(ExitEventArgs e)
{
_application.Shutdown();
await _host.StopAsync();
_host.Dispose();
Log.CloseAndFlush();
}
private void Initialize(IServiceProvider serviceProvider)
{
_application.Initialize(serviceProvider);
}
private IHost CreateHostBuilder()
{
return Host
.CreateDefaultBuilder(null)
.UseAutofac()
.UseSerilog()
.ConfigureServices((hostContext, services) =>
{
services.AddApplication<MyProjectNameModule>();
}).Build();
}
}
}

12
templates/wpf/src/MyCompanyName.MyProjectName/MainWindow.xaml.cs

@ -1,17 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MyCompanyName.MyProjectName
{

2
templates/wpf/src/MyCompanyName.MyProjectName/MyCompanyName.MyProjectName.csproj

@ -14,7 +14,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.*" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.1.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.1.2" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
</ItemGroup>

Loading…
Cancel
Save