Browse Source

added logging

pull/9909/head
Halil İbrahim Kalkan 5 years ago
parent
commit
889a48cc85
  1. 10
      test/DistEvents/DistDemoApp/DistDemoApp.csproj
  2. 2
      test/DistEvents/DistDemoApp/DistDemoAppModule.cs
  3. 39
      test/DistEvents/DistDemoApp/MyProjectNameHostedService.cs
  4. 60
      test/DistEvents/DistDemoApp/Program.cs
  5. 2
      test/DistEvents/DistDemoApp/TodoEventHandler.cs
  6. 2
      test/DistEvents/DistDemoApp/TodoSummary.cs

10
test/DistEvents/DistDemoApp/DistDemoApp.csproj

@ -4,7 +4,15 @@
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.*" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\src\Volo.Abp.EntityFrameworkCore.SqlServer\Volo.Abp.EntityFrameworkCore.SqlServer.csproj" />
<ProjectReference Include="..\..\..\framework\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />

2
test/DistEvents/DistDemoApp/DistDemoAppModule.cs

@ -15,6 +15,8 @@ namespace DistDemoApp
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddHostedService<MyProjectNameHostedService>();
context.Services.AddAbpDbContext<TodoDbContext>(options =>
{
options.AddDefaultRepositories();

39
test/DistEvents/DistDemoApp/MyProjectNameHostedService.cs

@ -0,0 +1,39 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Volo.Abp;
namespace DistDemoApp
{
public class MyProjectNameHostedService : IHostedService
{
private readonly IAbpApplicationWithExternalServiceProvider _application;
private readonly IServiceProvider _serviceProvider;
private readonly DemoService _demoService;
public MyProjectNameHostedService(
IAbpApplicationWithExternalServiceProvider application,
IServiceProvider serviceProvider,
DemoService demoService)
{
_application = application;
_serviceProvider = serviceProvider;
_demoService = demoService;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
_application.Initialize(_serviceProvider);
await _demoService.CreateTodoItemAsync();
}
public Task StopAsync(CancellationToken cancellationToken)
{
_application.Shutdown();
return Task.CompletedTask;
}
}
}

60
test/DistEvents/DistDemoApp/Program.cs

@ -1,29 +1,57 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
namespace DistDemoApp
{
class Program
public class Program
{
static async Task Main(string[] args)
public static async Task<int> Main(string[] args)
{
using (var application = AbpApplicationFactory.Create<DistDemoAppModule>(opts =>
Log.Logger = new LoggerConfiguration()
#if DEBUG
.MinimumLevel.Debug()
#else
.MinimumLevel.Information()
#endif
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Async(c => c.File("Logs/logs.txt"))
.WriteTo.Async(c => c.Console())
.CreateLogger();
try
{
opts.UseAutofac();
}))
Log.Information("Starting console host.");
await CreateHostBuilder(args).RunConsoleAsync();
return 0;
}
catch (Exception ex)
{
application.Initialize();
var demoService = application.ServiceProvider.GetRequiredService<DemoService>();
await demoService.CreateTodoItemAsync();
Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
application.Shutdown();
Log.Fatal(ex, "Host terminated unexpectedly!");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
internal static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseAutofac()
.UseSerilog()
.ConfigureAppConfiguration((context, config) =>
{
//setup your additional configuration sources
})
.ConfigureServices((hostContext, services) =>
{
services.AddApplication<DistDemoAppModule>();
});
}
}
}

2
test/DistEvents/DistDemoApp/TodoEventHandler.cs

@ -32,7 +32,7 @@ namespace DistDemoApp
if (todoSummary == null)
{
await _todoSummaryRepository.InsertAsync(new TodoSummary(dateTime));
todoSummary = await _todoSummaryRepository.InsertAsync(new TodoSummary(dateTime));
}
else
{

2
test/DistEvents/DistDemoApp/TodoSummary.cs

@ -35,7 +35,7 @@ namespace DistDemoApp
public override string ToString()
{
return $"{base.ToString()}, {Year}-{Month:2}-{Day:2}: {TotalCount}";
return $"{base.ToString()}, {Year}-{Month:00}-{Day:00}: {TotalCount}";
}
}
}
Loading…
Cancel
Save