baseline: 小军 WMS 二开代码原始快照(含 SynOABusiness.cs Debugger.Break)

This commit is contained in:
Gaussian committed 2026-11-06 18:38:38 +08:00
commit c2b0e7be0d
6522 files changed
+3530643

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
using Opsoft.NetCore.O3App.Shared;
using Serilog.Core;
using Serilog.Events;
namespace Opsoft.NetCore.O3App.Infrastructure
{
public class SkynetEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddPropertyIfAbsent(new LogEventProperty("ThreadId", new ScalarValue(System.Environment.CurrentManagedThreadId)));
logEvent.AddPropertyIfAbsent(new LogEventProperty("SkynetLevel", new ScalarValue(GetSkynetLevel(logEvent.Level))));
}
private static string GetSkynetLevel(LogEventLevel level)
{
return level == LogEventLevel.Information || level == LogEventLevel.Warning ? level.ToString().Left(4) : level.ToString();
}
}
}
+158
View File
@@ -0,0 +1,158 @@
using Opsoft.NetCore.O3App.Domain;
using Opsoft.NetCore.O3App.Shared;
using System;
using System.Runtime.CompilerServices;
namespace Opsoft.NetCore.O3App.Infrastructure
{
public class SkynetLogger<T> : ILogger<T>
{
private readonly string module = "";
private readonly string category = "";
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory;
private readonly ITraceContextAccessor traceContextAccessor;
SerilogService logService;
public SkynetLogger(Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, ITraceContextAccessor traceContextAccessor)
{
this.loggerFactory = Guard.ArgumentNotNull(loggerFactory, nameof(loggerFactory));
this.traceContextAccessor = Guard.ArgumentNotNull(traceContextAccessor, nameof(traceContextAccessor));
var type = typeof(T);
this.module = type.Namespace;
this.category = type.Name;
this.logService = new SerilogService(loggerFactory.CreateLogger(module));
}
public void Debug(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Debug(Format(message, module, category, callerName, filter1, filter2));
}
public void Debug(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Debug(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Error(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Error(Format(message, module, category, callerName, filter1, filter2));
}
public void Error(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Error(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Error(Exception ex, string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Error(ex, Format(message, module, category, callerName, filter1, filter2));
}
public void Error(Exception ex, object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Error(ex, Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Fatal(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Fatal(Format(message, module, category, callerName, filter1, filter2));
}
public void Fatal(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Fatal(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Fatal(Exception ex, string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Fatal(ex, Format(message, module, category, callerName, filter1, filter2));
}
public void Fatal(Exception ex, object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Fatal(ex, Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Info(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Info(Format(message, module, category, callerName, filter1, filter2));
}
public void Info(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Info(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Warn(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Warn(Format(message, module, category, callerName, filter1, filter2));
}
public void Warn(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Warn(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Warn(Exception ex, object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Warn(ex, Format(messageObject, module, category, callerName, filter1, filter2));
}
private string Format(object message, string module = null, string category = null, string subCategory = null, string filter1 = null, string filter2 = null)
{
string messageText = message.ToString();
if (message.GetType().IsComplexType())
{
messageText = message.ToJson(new Newtonsoft.Json.JsonSerializerSettings()
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
});
}
string traceId = traceContextAccessor.TraceContext?.TraceId ?? string.Empty;
if (traceId.NotNullOrWhiteSpace())
{
traceId = $"$apmTxId:{traceId}$";
}
return $"{traceId} [{module}][{category}][{subCategory}][{filter1}][{filter2}]{messageText}".TrimStart();
}
public void Info(string module, string category, string subCategory, string logContent, string filter1, string filter2)
{
logService.Info(Format(logContent, module, category, subCategory, filter1, filter2));
}
public void Error(string module, string category, string subCategory, string logContent, string filter1, string filter2)
{
logService.Error(Format(logContent, module, category, subCategory, filter1, filter2));
}
class SerilogService
{
Microsoft.Extensions.Logging.ILogger logger;
public SerilogService(Microsoft.Extensions.Logging.ILogger logger)
{
this.logger = Guard.ArgumentNotNull(logger, nameof(logger));
}
internal void Debug(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogDebug(logger, v);
internal void Error(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogError(logger, v);
internal void Error(Exception ex, string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogError(logger, ex, v);
internal void Fatal(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogCritical(logger, v);
internal void Fatal(Exception ex, string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogCritical(logger, ex, v);
internal void Info(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogInformation(logger, v);
internal void Warn(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogWarning(logger, v);
internal void Warn(Exception ex, string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogWarning(logger, ex, v);
}
}
}
+15
View File
@@ -0,0 +1,15 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Opsoft.NetCore.O3App.Shared;
namespace Opsoft.NetCore.O3App.Infrastructure
{
public static class SkynetLoggerExtensions
{
public static IServiceCollection AddSkynetLogger(this IServiceCollection services)
{
services.TryAddSingleton(typeof(ILogger<>), typeof(SkynetLogger<>));
return services;
}
}
}
+68
View File
@@ -0,0 +1,68 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Opsoft.NetCore.O3App.Shared;
using Serilog.Core;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Opsoft.NetCore.O3App.Infrastructure
{
public class SkynetLoggerFilter : ILogEventFilter
{
private const string SkynetLoggerFilterOptionKey = "LoggerFilters";
private readonly int defaultLogLevel = LogEventLevel.Verbose.ToInt32();
private readonly IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
private readonly IConfiguration configuration;
public SkynetLoggerFilter(IConfiguration configuration)
{
this.configuration = Guard.ArgumentNotNull(configuration, nameof(configuration));
}
public bool IsEnabled(LogEvent logEvent)
{
var filterOptions = GetCachedSkynetLoggerFilterOptions();
if (filterOptions.IsNullOrEmpty())
{
return true;
}
int level = logEvent.Level.ToInt32();
logEvent.Properties.TryGetValue(Serilog.Core.Constants.SourceContextPropertyName, out LogEventPropertyValue propertyValue);
if (propertyValue is ScalarValue scalar && scalar.Value is String)
{
var cat = (String)scalar.Value;
var matchedFilter = filterOptions.FirstOrDefault(opt => cat.StartsWith(opt.Catogery, StringComparison.OrdinalIgnoreCase));
return level >= (matchedFilter?.MinLevel ?? defaultLogLevel);
}
return true;
}
private List<SkynetLoggerFilterOption> GetCachedSkynetLoggerFilterOptions()
{
if (!memoryCache.TryGetValue(SkynetLoggerFilterOptionKey, out List<SkynetLoggerFilterOption> filters))
{
filters = GetSkynetLoggerFilterOptions();
memoryCache.Set(SkynetLoggerFilterOptionKey, filters, DateTimeOffset.Now.AddMinutes(5));
}
return filters;
}
private List<SkynetLoggerFilterOption> GetSkynetLoggerFilterOptions()
{
var filterOptions = (configuration[SkynetLoggerFilterOptionKey] ?? "[]").FromJson<List<SkynetLoggerFilterOption>>();
filterOptions.RemoveAll(opt => opt.Catogery.IsNullOrWhiteSpace());
return filterOptions;
}
}
public class SkynetLoggerFilterOption
{
public string Catogery { get; set; }
public int MinLevel { get; set; }
}
}
+23
View File
@@ -0,0 +1,23 @@
using Opsoft.NetCore.O3App.Shared;
using Serilog.Events;
using System.IO;
namespace Opsoft.NetCore.O3App.Infrastructure
{
public class SkynetLoggerFormatter : Serilog.Formatting.ITextFormatter
{
public void Format(LogEvent logEvent, TextWriter output)
{
output.WriteLine($"{logEvent.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.fff")} {GetSkynetLevel(logEvent.Level)} {logEvent.MessageTemplate.Render(logEvent.Properties)}");
if (logEvent.Exception != null)
{
output.WriteLine(logEvent.Exception);
}
}
private static string GetSkynetLevel(LogEventLevel level)
{
return level == LogEventLevel.Information || level == LogEventLevel.Warning ? level.ToString().Left(4) : level.ToString();
}
}
}
@@ -0,0 +1,23 @@
using Opsoft.NetCore.O3App.Domain;
using Opsoft.NetCore.O3App.Domain.Models;
using System.Threading;
namespace Opsoft.NetCore.O3App.Infrastructure
{
/// <inheritdoc />
public class TraceContextAccessor : ITraceContextAccessor
{
private static AsyncLocal<TraceContext> traceContext = new AsyncLocal<TraceContext>();
private volatile int transactionSequence;
/// <inheritdoc />
public TraceContext TraceContext
{
get => traceContext.Value;
set => traceContext.Value = value;
}
/// <inheritdoc />
public long TransactionSequence => Interlocked.Increment(ref transactionSequence);
}
}
@@ -0,0 +1,50 @@
using Opsoft.NetCore.O3App.Domain;
using Opsoft.NetCore.O3App.Domain.Models;
namespace Opsoft.NetCore.O3App.Infrastructure
{
/// <inheritdoc />
public class TraceContextFactory : ITraceContextFactory
{
private readonly ITraceContextAccessor traceContextAccessor;
/// <summary>
/// Initializes a new instance of the <see cref="T:TraceId.TraceContextFactory" /> class.
/// </summary>
public TraceContextFactory()
: this(null)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="TraceContextFactory"/> class.
/// </summary>
/// <param name="traceContextAccessor">The <see cref="ITraceContextAccessor"/> through which the <see cref="TraceContext"/> will be set.</param>
public TraceContextFactory(ITraceContextAccessor traceContextAccessor)
{
this.traceContextAccessor = traceContextAccessor;
}
/// <inheritdoc />
public TraceContext Create(string traceId, string header)
{
var traceContext = new TraceContext(traceId, header);
if (traceContextAccessor != null)
{
traceContextAccessor.TraceContext = traceContext;
}
return traceContext;
}
/// <inheritdoc />
public void Dispose()
{
if (traceContextAccessor != null)
{
traceContextAccessor.TraceContext = null;
}
}
}
}
+73
View File
@@ -0,0 +1,73 @@
using Microsoft.AspNetCore.Builder;
using Opsoft.NetCore.O3App.Domain;
using Opsoft.NetCore.O3App.Shared;
using System;
namespace Opsoft.NetCore.O3App.Infrastructure
{
/// <summary>
/// Extension methods for the TraceIdMiddleware.
/// </summary>
public static class TraceIdExtensions
{
/// <summary>
/// Enables Trace IDs for the request.
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseTraceId(new TraceIdOptions());
}
/// <summary>
/// Enables Trace IDs for the request.
/// </summary>
/// <param name="app"></param>
/// <param name="header">The header field name to use for the Trace ID.</param>
/// <returns></returns>
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app, string header)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseTraceId(new TraceIdOptions
{
Header = header
});
}
/// <summary>
/// Enables Trace IDs for the request.
/// </summary>
/// <param name="app"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app, TraceIdOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (app.ApplicationServices.GetService(typeof(ITraceContextFactory)) == null)
{
throw new InvalidOperationException("Unable to find the required services. You must call the AddTraceId method in ConfigureServices in the application startup code.");
}
return app.UseMiddleware<TraceIdMiddleware>(Microsoft.Extensions.Options.Options.Create(options));
}
}
}
+89
View File
@@ -0,0 +1,89 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Opsoft.NetCore.O3App.Domain;
using Opsoft.NetCore.O3App.Domain.Models;
using Opsoft.NetCore.O3App.Shared;
using System;
using System.Threading.Tasks;
namespace Opsoft.NetCore.O3App.Infrastructure
{
/// <summary>
/// Middleware which attempts to reads / creates a Trace ID that can then be used in logs and
/// passed to upstream requests.
/// </summary>
public class TraceIdMiddleware
{
private readonly RequestDelegate next;
private readonly TraceIdOptions options;
private readonly ITraceContextAccessor traceContextAccessor;
/// <summary>
/// Creates a new instance of the TraceIdMiddleware.
/// </summary>
/// <param name="next">The next middleware in the pipeline.</param>
/// <param name="options">The configuration options.</param>
public TraceIdMiddleware(RequestDelegate next, IOptions<TraceIdOptions> options, ITraceContextAccessor traceContextAccessor)
{
this.next = next ?? throw new ArgumentNullException(nameof(next));
this.options = options.Value ?? throw new ArgumentNullException(nameof(options));
this.traceContextAccessor = traceContextAccessor ?? throw new ArgumentNullException(nameof(traceContextAccessor));
}
/// <summary>
/// Processes a request to synchronise TraceIdentifier and Trace ID headers. Also creates a
/// <see cref="TraceContext"/> for the current request and disposes of it when the request is completing.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
/// <param name="traceContextFactory">The <see cref="ITraceContextFactory"/> which can create a <see cref="TraceContext"/>.</param>
public async Task Invoke(HttpContext context, ITraceContextFactory traceContextFactory)
{
var traceId = GetTraceId(context);
if (options.UpdateTraceIdentifier)
context.TraceIdentifier = traceId;
traceContextFactory.Create(traceId, options.Header);
if (options.IncludeInResponse)
{
// apply the Trace ID to the response header for client side tracking
context.Response.OnStarting(() =>
{
if (!context.Response.Headers.ContainsKey(options.Header))
{
context.Response.Headers.Add(options.Header, traceId);
}
return Task.CompletedTask;
});
}
await next(context);
traceContextFactory.Dispose();
}
private StringValues GetTraceId(HttpContext context)
{
var traceIdFoundInRequestHeader = context.Request.Headers.TryGetValue(options.Header, out var traceId);
if (RequiresGenerationOfTraceId(traceIdFoundInRequestHeader, traceId))
traceId = GenerateTraceId(context.TraceIdentifier);
return traceId;
}
private static bool RequiresGenerationOfTraceId(bool idInHeader, StringValues idFromHeader) =>
!idInHeader || StringValues.IsNullOrEmpty(idFromHeader);
private StringValues GenerateTraceId(string traceIdentifier)
{
return new TransactionId(
TraceUtility.GenerateAgentId(),
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
traceContextAccessor.TransactionSequence).ToString();
}
}
}
@@ -0,0 +1,24 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Opsoft.NetCore.O3App.Domain;
namespace Opsoft.NetCore.O3App.Infrastructure
{
/// <summary>
/// Extensions on the <see cref="IServiceCollection"/>.
/// </summary>
public static class TraceIdServiceExtensions
{
/// <summary>
/// Adds required services to support the Trace ID functionality.
/// </summary>
/// <param name="serviceCollection"></param>
public static IServiceCollection AddTraceId(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<ITraceContextAccessor, TraceContextAccessor>();
serviceCollection.TryAddTransient<ITraceContextFactory, TraceContextFactory>();
return serviceCollection;
}
}
}