-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAppMixins.cs
More file actions
89 lines (81 loc) · 3.85 KB
/
Copy pathAppMixins.cs
File metadata and controls
89 lines (81 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using MainCore.Behaviors;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Templates;
using Splat.Microsoft.Extensions.DependencyInjection;
[assembly: Behaviors(
typeof(AccountDataLoggingBehavior<,>),
typeof(TaskNameLoggingBehavior<,>),
typeof(CommandLoggingBehavior<,>),
typeof(ErrorLoggingBehavior<,>),
typeof(AccountTaskBehavior<,>),
typeof(VillageTaskBehavior<,>)
)]
namespace MainCore
{
public static class AppMixins
{
private static IHostBuilder ConfigureLogging(this IHostBuilder hostBuilder) =>
hostBuilder.ConfigureServices(services =>
{
services.AddSerilog(c => c
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Filter.ByExcluding("SourceContext like 'ReactiveUI.POCOObservableForProperty' and Contains(@m, 'WhenAny will only return a single value')")
.WriteTo.Map("Account", "Other", (acc, wt) =>
{
wt.File(new ExpressionTemplate("[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"),
path: $"./logs/log-{acc}-.txt",
rollingInterval: RollingInterval.Day);
wt.LogSink();
})
.Enrich.FromLogContext());
});
private const string _connectionString = "DataSource=TBS.db;Cache=Shared";
private static IHostBuilder ConfigureDbContext(this IHostBuilder hostBuilder) =>
hostBuilder.ConfigureServices((hostContext, services) =>
{
services.AddDbContext<AppDbContext>(options => options
.EnableSensitiveDataLogging(hostContext.HostingEnvironment.IsDevelopment())
.UseSqlite(_connectionString, o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));
});
public static IHostBuilder ConfigureServices(this IHostBuilder hostBuilder) =>
hostBuilder.ConfigureServices(services =>
{
services.AddMainCore();
services.AddValidatorsFromAssembly(typeof(AppMixins).Assembly, ServiceLifetime.Singleton);
services.AddMainCoreBehaviors();
services.AddMainCoreHandlers();
services.AddScoped<IChromeBrowser>(sp =>
{
var dataService = sp.GetRequiredService<IDataService>();
if (dataService.AccountId == AccountId.Empty) throw new InvalidOperationException("AccountId is empty");
var chromeManager = sp.GetRequiredService<IChromeManager>();
var logger = Log
.ForContext("Account", dataService.AccountData)
.ForContext("AccountId", dataService.AccountId);
var browser = chromeManager.Get(dataService.AccountId);
browser.Logger = logger;
return browser;
});
});
private static IHostBuilder ConfigureSplatForMicrosoftDependencyResolver(this IHostBuilder hostBuilder) =>
hostBuilder.ConfigureServices((serviceCollection) =>
{
serviceCollection.UseMicrosoftDependencyResolver();
var resolver = Locator.CurrentMutable;
resolver.InitializeSplat();
resolver.InitializeReactiveUI();
});
public static IHostBuilder GetHostBuilder()
{
var hostBuilder = Host.CreateDefaultBuilder()
.ConfigureSplatForMicrosoftDependencyResolver()
.ConfigureLogging()
.ConfigureDbContext()
.ConfigureServices();
return hostBuilder;
}
}
}