add vite-proxy; add seq logging (optional)

This commit is contained in:
2026-03-25 15:38:53 +01:00
parent 6e7be47308
commit cc24429c6f
6 changed files with 212 additions and 3 deletions
+99
View File
@@ -0,0 +1,99 @@
using Microsoft.AspNetCore.Builder;
#if DEBUG
using ViteProxy;
#endif
namespace zero.Frontend;
public static class ZeroBuilderExtensions
{
public static ZeroBuilder AddVite(this ZeroBuilder builder)
{
#if DEBUG
builder.Services.AddViteProxy("Zero:Vite");
#endif
return builder;
}
}
public static class ViteProxyApplicationBuilderExtensions
{
public static IApplicationBuilder UseVite(this IApplicationBuilder app, string path = null, string configName = null)
{
#if DEBUG
app.UseViteStaticFiles(path, configName);
#endif
return app;
}
}
// internal class ZeroViteModule : ZeroModule
// {
// public override int ConfigureOrder { get; } = -1;
//
// public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
// {
// #if DEBUG
// services.AddViteProxy();
//
// services.AddHttpClient("vite-proxy")
// .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler
// {
// UseCookies = false,
// AllowAutoRedirect = false,
// AutomaticDecompression = System.Net.DecompressionMethods.None,
// PooledConnectionLifetime = TimeSpan.FromMinutes(5),
// PooledConnectionIdleTimeout = TimeSpan.FromMinutes(2),
// MaxConnectionsPerServer = 256
// });
// #endif
// }
//
//
// public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
// {
// #if DEBUG
// app.UseViteStaticFiles();
// #endif
// //
// // app.Use(async (context, next) =>
// // {
// // if (context.Request.Path.StartsWithSegments("/@viteproxy", out _))
// // {
// // var targetUri = new Uri($"http://localhost:5123{context.Request.Path}{context.Request.QueryString}");
// //
// // var client = context.RequestServices.GetRequiredService<IHttpClientFactory>()
// // .CreateClient("vite-proxy");
// //
// // using var requestMessage = new HttpRequestMessage(new HttpMethod(context.Request.Method), targetUri);
// //
// // if (context.Request.ContentLength > 0)
// // requestMessage.Content = new StreamContent(context.Request.Body);
// //
// // if (!string.IsNullOrEmpty(context.Request.ContentType) && requestMessage.Content != null)
// // requestMessage.Content.Headers.TryAddWithoutValidation("Content-Type", context.Request.ContentType);
// //
// // using var responseMessage = await client.SendAsync(
// // requestMessage,
// // HttpCompletionOption.ResponseHeadersRead,
// // context.RequestAborted);
// //
// // context.Response.StatusCode = (int)responseMessage.StatusCode;
// //
// // foreach (var header in responseMessage.Headers)
// // context.Response.Headers[header.Key] = header.Value.ToArray();
// //
// // foreach (var header in responseMessage.Content.Headers)
// // context.Response.Headers[header.Key] = header.Value.ToArray();
// //
// // context.Response.Headers.Remove("transfer-encoding");
// // context.Response.Headers.Remove("connection");
// //
// // await responseMessage.Content.CopyToAsync(context.Response.Body, context.RequestAborted);
// // return;
// // }
// //
// // await next();
// // });
// }
// }
+33
View File
@@ -0,0 +1,33 @@
using System.Reflection;
using Microsoft.Extensions.Logging;
namespace zero.Logging;
public class LogLevelOverrides : Dictionary<string, LogLevel>
{
public LogLevelOverrides()//IHostEnvironment env)
{
this["zero"] = LogLevel.Debug;
this["zero.Routing"] = LogLevel.Debug;
// if (env.IsDevelopment())
// {
// this["zero"] = LogLevel.Debug;
// this["zero.Routing"] = LogLevel.Debug;
// }
// else
// {
// this["zero"] = LogLevel.Debug;
// }
this["SixLabors"] = LogLevel.Warning;
this["Quartz"] = LogLevel.Warning;
this["Microsoft.AspNetCore"] = LogLevel.Warning;
string executingAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
if (executingAssemblyName != null)
{
this[executingAssemblyName] = LogLevel.Debug;
}
}
}
+40
View File
@@ -0,0 +1,40 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace zero.Logging;
public class ZeroLoggingModule : ZeroModule
{
public override int Order { get; } = -100;
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddLogging(builder =>
{
// get seq configuration
IConfigurationSection seqConfig = configuration.GetSection("Zero:Seq");
ZeroSeqOptions seqOptions = seqConfig.Get<ZeroSeqOptions>();
Console.WriteLine(seqOptions.ApiKey);
Console.WriteLine(seqOptions.ServerUrl);
Console.WriteLine("use seq: " + seqConfig.Exists());
// default level
builder.SetMinimumLevel(seqOptions.MinimumLevel);
// apply log level overrides from zero
foreach (KeyValuePair<string, LogLevel> levelOverride in seqOptions.LevelOverrides)
{
builder.AddFilter(levelOverride.Key, levelOverride.Value);
}
// add optional seq sink
if (seqConfig.Exists())
{
builder.AddSeq(seqOptions.ServerUrl, seqOptions.ApiKey, seqOptions.Enrichers);
}
});
}
}
+32
View File
@@ -0,0 +1,32 @@
using Microsoft.Extensions.Logging;
using Seq.Extensions.Logging;
namespace zero.Logging;
public class ZeroSeqOptions
{
/// <summary>
/// URL to the Seq server
/// </summary>
public string ServerUrl { get; set; } = "http://localhost:5341";
/// <summary>
/// A Seq API key to authenticate or tag messages from the logger
/// </summary>
public string ApiKey { get; set; }
/// <summary>
/// The level below which events will be suppressed
/// </summary>
public LogLevel MinimumLevel { get; set; } = LogLevel.Information;
/// <summary>
/// A dictionary mapping logger name prefixes to minimum logging levels.
/// </summary>
public Dictionary<string, LogLevel> LevelOverrides { get; set; } = new LogLevelOverrides();
/// <summary>
/// A collection of enrichers to apply.
/// </summary>
public List<Action<EnrichingEvent>> Enrichers { get; set; } = [];
}
+3 -3
View File
@@ -1,9 +1,8 @@
using FluentValidation;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using zero.Logging;
using zero.Mails;
using zero.Metadata;
using zero.Mvc;
@@ -52,7 +51,8 @@ public class ZeroBuilder
// adds and discovers additional and built-in assemblies
new AssemblyDiscovery(Mvc).Execute(_startupOptions.AssemblyDiscoveryRules);
Modules.Add<ZeroLoggingModule>();
Modules.Add<ZeroCommunicationModule>();
Modules.Add<ZeroMvcModule>();
Modules.Add<ZeroConfigurationModule>();
+5
View File
@@ -18,9 +18,14 @@
<PackageReference Include="FluentValidation" Version="12.1.1" />
<PackageReference Include="Postmark" Version="5.3.0" />
<PackageReference Include="PowCapServer.Core" Version="2.0.0" />
<PackageReference Include="Seq.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="SixLabors.ImageSharp.Web" Version="3.2.0" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<ProjectReference Include="..\..\ViteProxy\src\ViteProxy.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\**\*" />
</ItemGroup>