Browse Source

Make wpf template logger work

pull/7934/head
liangshiwei 6 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;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -22,19 +17,6 @@ namespace MyCompanyName.MyProjectName
private readonly IAbpApplicationWithExternalServiceProvider _application; private readonly IAbpApplicationWithExternalServiceProvider _application;
public App() 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() Log.Logger = new LoggerConfiguration()
#if DEBUG #if DEBUG
@ -47,6 +29,12 @@ namespace MyCompanyName.MyProjectName
.WriteTo.Async(c => c.File("Logs/logs.txt")) .WriteTo.Async(c => c.File("Logs/logs.txt"))
.CreateLogger(); .CreateLogger();
_host = CreateHostBuilder();
_application = _host.Services.GetService<IAbpApplicationWithExternalServiceProvider>();
}
protected override async void OnStartup(StartupEventArgs e)
{
try try
{ {
Log.Information("Starting WPF host."); Log.Information("Starting WPF host.");
@ -60,22 +48,31 @@ namespace MyCompanyName.MyProjectName
{ {
Log.Fatal(ex, "Host terminated unexpectedly!"); 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(); _application.Shutdown();
await _host.StopAsync(); await _host.StopAsync();
_host.Dispose(); _host.Dispose();
Log.CloseAndFlush();
} }
private void Initialize(IServiceProvider serviceProvider) private void Initialize(IServiceProvider serviceProvider)
{ {
_application.Initialize(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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; 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 namespace MyCompanyName.MyProjectName
{ {

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

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

Loading…
Cancel
Save