Compare commits
4 Commits
117d6fea10
...
9c162e1752
| Author | SHA1 | Date | |
|---|---|---|---|
| 9c162e1752 | |||
| e834ba073f | |||
| 8a1eb23f05 | |||
| 64d78a7cd5 |
@@ -1,55 +1,102 @@
|
|||||||
using Microsoft.AspNetCore.Hosting;
|
using System.Reflection;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using TemplateNETService.Business;
|
using TemplateNETService.Business;
|
||||||
using TemplateNETService.Models;
|
using TemplateNETService.Models;
|
||||||
using Log = Serilog.Log;
|
|
||||||
|
|
||||||
namespace TemplateNETService;
|
namespace TemplateNETService;
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
RunWebApp(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RunWebApp(string[] args)
|
||||||
|
{
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
ConfigureConfiguration(builder);
|
||||||
|
ConfigureLogging(builder);
|
||||||
|
ConfigureServices(builder.Services);
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
ConfigureApp(app);
|
||||||
|
app.Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ConfigureConfiguration(WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
builder.Configuration
|
||||||
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||||
|
.AddEnvironmentVariables();
|
||||||
|
var generalConfig = builder.Configuration.GetSection("General").Get<General>();
|
||||||
|
if (generalConfig == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Configuration section 'General' is missing or invalid in appsettings.json");
|
||||||
|
}
|
||||||
|
Config.general = generalConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ConfigureLogging(WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory("_LOG");
|
Directory.CreateDirectory("_LOG");
|
||||||
|
|
||||||
Log.Logger = new LoggerConfiguration()
|
Log.Logger = new LoggerConfiguration()
|
||||||
.WriteTo.Console()
|
.WriteTo.Console()
|
||||||
.MinimumLevel.Debug()
|
.MinimumLevel.Debug()
|
||||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Error) // Für Microsoft-Komponenten nur Warning+
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Error)
|
||||||
.WriteTo.File(Path.Combine(AppContext.BaseDirectory, "_LOG/TEST.log"), rollingInterval: RollingInterval.Day)
|
.WriteTo.File(
|
||||||
.Filter.ByExcluding(logEvent => logEvent.Level == LogEventLevel.Information)
|
Path.Combine(AppContext.BaseDirectory, "_LOG/TemplateNETService.log"),
|
||||||
|
rollingInterval: RollingInterval.Day,
|
||||||
|
fileSizeLimitBytes: 100000000,
|
||||||
|
rollOnFileSizeLimit: true,
|
||||||
|
retainedFileTimeLimit: TimeSpan.FromDays(14)
|
||||||
|
)
|
||||||
|
.WriteTo.File(
|
||||||
|
Path.Combine(AppContext.BaseDirectory, "_LOG/TemplateNETService.error.log"),
|
||||||
|
rollingInterval: RollingInterval.Day,
|
||||||
|
restrictedToMinimumLevel: LogEventLevel.Error
|
||||||
|
)
|
||||||
.CreateLogger();
|
.CreateLogger();
|
||||||
|
|
||||||
CreateHostBuilder(args).Build().Run();
|
builder.Logging.ClearProviders();
|
||||||
|
builder.Logging.AddSerilog(Log.Logger);
|
||||||
|
builder.Host.UseSerilog();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); });
|
||||||
|
|
||||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
services.AddEndpointsApiExplorer();
|
||||||
Host.CreateDefaultBuilder(args)
|
services.AddSwaggerGen(c =>
|
||||||
.UseWindowsService()
|
{
|
||||||
.ConfigureAppConfiguration((context, config) =>
|
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||||
{
|
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
||||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
});
|
||||||
})
|
|
||||||
.UseSerilog()
|
|
||||||
.ConfigureWebHostDefaults(webBuilder =>
|
|
||||||
{
|
|
||||||
var config = new ConfigurationBuilder()
|
|
||||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
Config.general = config.GetSection("General").Get<General>();
|
services.AddWindowsService();
|
||||||
|
services.AddHostedService<BaseWorker>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ConfigureApp(WebApplication app)
|
||||||
|
{
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI();
|
||||||
|
|
||||||
webBuilder.UseStartup<Startup>();
|
app.UseHttpsRedirection();
|
||||||
webBuilder.UseKestrel(options =>
|
app.UseAuthorization();
|
||||||
{
|
app.MapControllers();
|
||||||
options.ListenAnyIP(Config.general.ThisPort);
|
|
||||||
})
|
app.Urls.Add($"http://*:{Config.general.ThisPort}");
|
||||||
.ConfigureServices(services =>
|
}
|
||||||
{
|
|
||||||
services.AddHostedService<BaseWorker>();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
|
|
||||||
namespace TemplateNETService;
|
|
||||||
|
|
||||||
public class Startup
|
|
||||||
{
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
|
||||||
{
|
|
||||||
services.AddControllers();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
||||||
{
|
|
||||||
if (env.IsDevelopment())
|
|
||||||
app.UseDeveloperExceptionPage();
|
|
||||||
|
|
||||||
app.UseRouting();
|
|
||||||
app.UseEndpoints(endpoints =>
|
|
||||||
{
|
|
||||||
endpoints.MapControllers();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -5,17 +5,21 @@
|
|||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore" Version="2.3.0" />
|
<PackageReference Include="Microsoft.AspNetCore" Version="2.3.9" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.4" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.23" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.0.4" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.2" />
|
||||||
<PackageReference Include="Serilog" Version="4.2.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="10.0.2" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
<PackageReference Include="Serilog" Version="4.3.0" />
|
||||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
|
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
<PackageReference Include="Serilog.Extensions.Hosting" Version="10.0.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="10.1.0" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user