Files
xLoggingService/DI/XDIHelperExtension.cs
saherelm def0807fdd 2.1) Middleware جدید
- فایل: MIddlewares/XRequestLoggingMiddleware.cs (NEW)
- ویژگی: Simple/Full logging modes
- قابلیت: HTTP request/response logging، exception handling

⚠️ توجه: نام پوشه "MIddlewares" typo است - باید تصحیح شود

2.2) توسعه DI
- متد: AddXTelemetryLogger() - logging configuration
- متد: AddXTelemetry() - host builder extension
- متد: UseXRequestLogger() - middleware registration

2.3) به‌روزرسانی Dependencies
- OpenTelemetry.Extensions.Hosting v1.6.0
- OpenTelemetry.Instrumentation.Http v1.6.0
- OpenTelemetry.Instrumentation.AspNetCore v1.6.0
- OpenTelemetry.Exporter.OpenTelemetryProtocol v1.6.0
- Microsoft.Extensions.Logging.Console v5.0.0
2026-11-09 12:50:04 +03:30

273 lines
8.2 KiB
C#

using System;
using OpenTelemetry.Logs;
using OpenTelemetry.Trace;
using xCommons.Extensions;
using xExceptions.Constants;
using OpenTelemetry.Resources;
using xLoggingService.Writers;
using xEventService.Interfaces;
using xLoggingService.Providers;
using xLoggingService.Constants;
using xLoggingService.Interfaces;
using xLoggingService.MIddlewares;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Builder;
using xLoggingService.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace xLoggingService.DI
{
public static class XDIHelperExtension
{
/// <summary>
/// Retrieve Logging Service Configuration ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XLoggingServiceConfiguration GetXLoggingServiceConfiguration(
this IConfiguration source
)
{
//
var section = source
.GetSection(ConfigurationNodeNames.LOGGING_SERVICE_NODE);
//
var result = section.Get<XLoggingServiceConfiguration>();
if (result.IsNull())
{
result = new();
}
//
return result;
}
/// <summary>
/// Extract Service Configurations and Register it ...
/// </summary>
/// <param name="source"></param>
/// <param name="configuration"></param>
public static void AddXLoggingServiceConfiguration(
this IServiceCollection source,
IConfiguration configuration
)
{
//
var config = configuration.GetXLoggingServiceConfiguration();
source.AddXLoggingServiceConfiguration(config);
}
/// <summary>
/// Register Service Configuration ...
/// </summary>
/// <param name="source"></param>
/// <param name="configuration"></param>
public static void AddXLoggingServiceConfiguration(
this IServiceCollection source,
XLoggingServiceConfiguration configuration
)
{
//
if (configuration.IsNull())
{
//
Log($"Invalid Configuration ...");
XException.InvalidConfiguration.Throw();
}
//
source.AddSingleton(configuration);
Log($"Logging Configuration Registered Successfully ...");
}
/// <summary>
/// Register Logging Service ...
/// </summary>
/// <param name="source"></param>
/// <param name="configuration"></param>
public static void AddXLoggingService(
this IServiceCollection source,
IConfiguration configuration
)
{
//
var config = configuration.GetXLoggingServiceConfiguration();
source.AddLoggingService(
configuration: config,
lifeTime: ServiceLifetime.Scoped
);
}
/// <summary>
/// Enable Telemetry Service on Logging ...
/// </summary>
/// <param name="builder"></param>
public static void AddXTelemetryLogger(
this ILoggingBuilder builder
)
{
//
AppContext.SetSwitch(
"System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
//
// Clear Providers ...
builder.ClearProviders();
builder.AddConsole();
builder.AddOpenTelemetry(options =>
{
//
options.SetResourceBuilder(
ResourceBuilder
.CreateDefault()
.AddService(nameof(xLoggingService)));
//
options.AddOtlpExporter();
});
}
/// <summary>
/// Add Telemetry Service to Services ...
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IHostBuilder AddXTelemetry(
this IHostBuilder builder
)
{
//
// Configure Services ...
builder = builder
.ConfigureServices((hostContext, services) =>
{
//
services
.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(nameof(xLoggingService)))
.WithTracing(tracing =>
tracing
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter());
});
//
return builder;
}
/// <summary>
/// Use Request Logging Middleware ...
/// Log all Requests, and it's Flow to Logger ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static IApplicationBuilder UseXRequestLogger(
this IApplicationBuilder source
)
{
return source.UseMiddleware<XRequestLoggingMiddleware>();
}
//
#region Private
/// <summary>
/// a LogTag for Service ...
/// </summary>
private static string XLogTag = XLoggingServiceConstants.XLoggingServiceDILogTag;
/// <summary>
/// print a log in Console ...
/// </summary>
/// <param name="message"></param>
private static void Log(string message)
{
Console.WriteLine($"{XLogTag} => {message}");
}
/// <summary>
/// Register Logging Service ...
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
/// <param name="lifeTime"></param>
private static void AddLoggingService(
this IServiceCollection services,
XLoggingServiceConfiguration configuration,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
//
// Validate ...
if (lifeTime.IsNull())
{
//
Log($"Invalid Service Lifetime ...");
XException.InvalidArgs.Throw();
}
//
// Register Service Configuration ...
services.AddXLoggingServiceConfiguration(configuration);
//
// File Log Provider ...
if (configuration.File.Enabled)
{
//
services.TryAddEnumerable(
ServiceDescriptor.Singleton<IXLogWriter, XFileLogWriter>());
//
Log("FileLogWriter registered.");
}
//
// Database Log Provider ...
if (configuration.Database.Enabled)
{
//
services.TryAddEnumerable(
ServiceDescriptor.Singleton<IXLogWriter, XDatabaseLogWriter>());
//
Log("DatabaseLogWriter registered.");
}
//
// Event Service Provider ...
if (configuration.Event.Enabled)
{
//
var eventService = services.GetRegisteredService<IXEventServiceProvider>();
if (eventService.IsNull())
{
//
Log("Event Service not Provided ...");
XException.InvalidConfiguration.Throw();
}
//
services.TryAddEnumerable(
ServiceDescriptor.Singleton<IXLogWriter, XEventLogWriter>());
//
Log("EventLogWriter registered.");
}
//
services.TryAddEnumerable(
ServiceDescriptor.Singleton<ILoggerProvider, XLoggerProvider>());
//
Log($"Logging Service registered successfully ...");
}
#endregion
}
}