tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ServiceCollectionExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Diagnostics;
3using System.Globalization;
4
5using Microsoft.Extensions.Configuration;
6using Microsoft.Extensions.DependencyInjection;
7using Microsoft.Extensions.DependencyInjection.Extensions;
8using Microsoft.Extensions.Logging;
9
10using Serilog;
11using Serilog.Configuration;
12using Serilog.Sinks.Elasticsearch;
13
20
22{
27 {
32
37
41 static Type? fileDownloaderType;
42
46 static ServiceDescriptor? additionalLoggerProvider;
47
55
61 where TProviderFactory : IProviderFactory
62 {
63 chatProviderFactoryType = typeof(TProviderFactory);
64 }
65
71 where TGitHubServiceFactory : IGitHubServiceFactory
72 {
73 gitHubServiceFactoryType = typeof(TGitHubServiceFactory);
74 }
75
81 where TFileDownloader : IFileDownloader
82 {
83 fileDownloaderType = typeof(TFileDownloader);
84 }
85
91 public static IServiceCollection AddFileDownloader(this IServiceCollection serviceCollection)
92 {
93 ArgumentNullException.ThrowIfNull(serviceCollection);
94
95 serviceCollection.AddSingleton(typeof(IFileDownloader), fileDownloaderType ?? throw new InvalidOperationException("fileDownloaderType not set!"));
96
97 return serviceCollection;
98 }
99
105 public static IServiceCollection AddGitHub(this IServiceCollection serviceCollection)
106 {
107 ArgumentNullException.ThrowIfNull(serviceCollection);
108
109 serviceCollection.AddSingleton<IGitHubClientFactory, GitHubClientFactory>();
110 serviceCollection.AddSingleton(typeof(IGitHubServiceFactory), gitHubServiceFactoryType ?? throw new InvalidOperationException("gitHubServiceFactoryType not set!"));
111
112 return serviceCollection;
113 }
114
120 where TLoggerProvider : class, ILoggerProvider
121 {
122 if (additionalLoggerProvider != null)
123 throw new InvalidOperationException("Cannot have multiple additionalLoggerProviders!");
124 additionalLoggerProvider = ServiceDescriptor.Singleton<ILoggerProvider, TLoggerProvider>();
125 }
126
132 public static IServiceCollection AddChatProviderFactory(this IServiceCollection serviceCollection)
133 {
134 ArgumentNullException.ThrowIfNull(serviceCollection);
135
136 return serviceCollection.AddSingleton(typeof(IProviderFactory), chatProviderFactoryType ?? throw new InvalidOperationException("chatProviderFactoryType not set!"));
137 }
138
146 public static IServiceCollection UseStandardConfig<TConfig>(this IServiceCollection serviceCollection, IConfiguration configuration)
147 where TConfig : class
148 {
149 ArgumentNullException.ThrowIfNull(serviceCollection);
150 ArgumentNullException.ThrowIfNull(configuration);
151
152 const string SectionFieldName = nameof(GeneralConfiguration.Section);
153
154 var configType = typeof(TConfig);
155 var sectionField = configType.GetField(SectionFieldName) ?? throw new InvalidOperationException(
156 String.Format(CultureInfo.InvariantCulture, "{0} has no {1} field!", configType, SectionFieldName));
157 var stringType = typeof(string);
158 if (sectionField.FieldType != stringType)
159 throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "{0} has invalid {1} field type, must be {2}!", configType, SectionFieldName, stringType));
160
161 var sectionName = (string)sectionField.GetValue(null)!;
162
163 return serviceCollection.Configure<TConfig>(configuration.GetSection(sectionName));
164 }
165
176 public static IServiceCollection SetupLogging(
177 this IServiceCollection serviceCollection,
178 Action<LoggerConfiguration> configurationAction,
179 Action<LoggerSinkConfiguration>? sinkConfigurationAction = null,
180 ElasticsearchSinkOptions? elasticsearchSinkOptions = null,
181 InternalConfiguration? internalConfiguration = null,
182 FileLoggingConfiguration? fileLoggingConfiguration = null)
183 {
184 if (internalConfiguration != null)
185 ArgumentNullException.ThrowIfNull(fileLoggingConfiguration);
186
187 return serviceCollection.AddLogging(builder =>
188 {
189 builder.ClearProviders();
190
191 var configuration = new LoggerConfiguration()
192 .MinimumLevel
193 .Verbose();
194
195 configurationAction?.Invoke(configuration);
196
197 configuration
198 .Enrich.FromLogContext()
199 .WriteTo
200 .Async(sinkConfiguration =>
201 {
202 var template = "[{Timestamp:HH:mm:ss}] {Level:w3}: {SourceContext:l} ("
203 + SerilogContextHelper.Template
204 + "){NewLine} {Message:lj}{NewLine}{Exception}";
205
206 if (!((internalConfiguration?.UsingSystemD ?? false) && !(fileLoggingConfiguration?.Disable ?? false)))
207 sinkConfiguration.Console(outputTemplate: template, formatProvider: CultureInfo.InvariantCulture);
208 sinkConfigurationAction?.Invoke(sinkConfiguration);
209 });
210
211 if (elasticsearchSinkOptions != null)
212 configuration.WriteTo.Elasticsearch(elasticsearchSinkOptions);
213
214 builder.AddSerilog(configuration.CreateLogger(), true);
215
216 if (Debugger.IsAttached)
217 builder.AddDebug();
218
219 if (additionalLoggerProvider != null)
220 builder.Services.TryAddEnumerable(additionalLoggerProvider);
221 });
222 }
223
230 public static void AddHub<THub, THubMethods>(this IServiceCollection services)
232 where THubMethods : class
233 {
234 ArgumentNullException.ThrowIfNull(services);
235
236 services.TryAddSingleton(typeof(ComprehensiveHubContext<,>));
237 services.AddSingleton<IConnectionMappedHubContext<THub, THubMethods>>(provider => provider.GetRequiredService<ComprehensiveHubContext<THub, THubMethods>>());
238 services.AddSingleton<IHubConnectionMapper<THub, THubMethods>>(provider => provider.GetRequiredService<ComprehensiveHubContext<THub, THubMethods>>());
239 }
240
244 static void UseDefaultServices()
245 {
246 UseChatProviderFactory<ProviderFactory>();
247 UseGitHubServiceFactory<GitHubServiceFactory>();
248 UseFileDownloader<FileDownloader>();
249 }
250 }
251}
const string Section
The key for the Microsoft.Extensions.Configuration.IConfigurationSection the GeneralConfiguration res...
Unstable configuration options used internally by TGS.
static void UseAdditionalLoggerProvider< TLoggerProvider >()
Add an additional ILoggerProvider to IServiceCollections that call SetupLogging(IServiceCollection,...
static void AddHub< THub, THubMethods >(this IServiceCollection services)
Attempt to add the given THub to services.
static void UseDefaultServices()
Set the modifiable services to their default types.
static IServiceCollection UseStandardConfig< TConfig >(this IServiceCollection serviceCollection, IConfiguration configuration)
Add a standard TConfig binding.
static ? Type fileDownloaderType
The IFileDownloader implementation used in calls to AddFileDownloader(IServiceCollection).
static void UseChatProviderFactory< TProviderFactory >()
Change the Type used as an implementation for calls to AddChatProviderFactory(IServiceCollection).
static ? Type gitHubServiceFactoryType
The IGitHubServiceFactory implementation used in calls to AddGitHub(IServiceCollection).
static IServiceCollection AddFileDownloader(this IServiceCollection serviceCollection)
Adds a IFileDownloader implementation to the given serviceCollection .
static IServiceCollection AddChatProviderFactory(this IServiceCollection serviceCollection)
Adds a IProviderFactory implementation to the given serviceCollection .
static ServiceCollectionExtensions()
Initializes static members of the ServiceCollectionExtensions class.
static IServiceCollection AddGitHub(this IServiceCollection serviceCollection)
Adds a IGitHubServiceFactory implementation to the given serviceCollection .
static IServiceCollection SetupLogging(this IServiceCollection serviceCollection, Action< LoggerConfiguration > configurationAction, Action< LoggerSinkConfiguration >? sinkConfigurationAction=null, ElasticsearchSinkOptions? elasticsearchSinkOptions=null, InternalConfiguration? internalConfiguration=null, FileLoggingConfiguration? fileLoggingConfiguration=null)
Clear previous providers and configure logging.
static ? Type chatProviderFactoryType
The IProviderFactory implementation used in calls to AddChatProviderFactory(IServiceCollection).
static ? ServiceDescriptor additionalLoggerProvider
A ServiceDescriptor for an additional ILoggerProvider to use.
static void UseFileDownloader< TFileDownloader >()
Change the Type used as an implementation for calls to AddGitHub(IServiceCollection).
static void UseGitHubServiceFactory< TGitHubServiceFactory >()
Change the Type used as an implementation for calls to AddGitHub(IServiceCollection).
An implementation of IHubContext<THub> with User connection ID mapping.
Base class for Hub<T>s that want to map their connection IDs to Models.PermissionSets.
A IHubContext<THub> that maps Users to their connection IDs.
Handles mapping connection IDs to Users for a given THub .