Skip to content

Commit

Permalink
Merge pull request #7934 from abpframework/liangshiwei/wpf
Browse files Browse the repository at this point in the history
Make WPF template logger work
  • Loading branch information
maliming committed Mar 4, 2021
2 parents 18df97f + 92d3e68 commit 8d393cc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
43 changes: 20 additions & 23 deletions templates/wpf/src/MyCompanyName.MyProjectName/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,19 +17,6 @@ public partial class App : Application
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
Expand All @@ -47,6 +29,12 @@ protected async override void OnStartup(StartupEventArgs e)
.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.");
Expand All @@ -60,22 +48,31 @@ protected async override void OnStartup(StartupEventArgs e)
{
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 changes: 0 additions & 12 deletions templates/wpf/src/MyCompanyName.MyProjectName/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 8d393cc

Please sign in to comment.