public static IServiceCollection AddApiAuditing(this IServiceCollection services) { IServiceProvider sp = services.BuildServiceProvider(); var configuration = sp.GetRequiredService <IConfiguration>(); var env = sp.GetRequiredService <IHostingEnvironment>(); var auditingConfiguration = new AuditingConfiguration(configuration); var auditRoot = auditingConfiguration.AuditingRoot; var minLevel = auditingConfiguration.MinLevel; var defaultAuditRoot = auditingConfiguration.GetDefaultAuditRoot(env); // If invalid directory was specified in the configuration. Reset to default if (!Directory.Exists(auditRoot)) { auditRoot = defaultAuditRoot; } if (!auditingConfiguration.Enabled) { // Disable auditing minLevel = (LogLevel)(1 + (int)LogEventLevel.Fatal); } AuditAttribute.Logger = new LoggerConfiguration() .MinimumLevel .Is(LoggingConfiguration.ToLogEventLevel(minLevel)) .WriteTo .RollingFile(Path.Combine(auditRoot, auditingConfiguration.FileName), retainedFileCountLimit: auditingConfiguration.MaxFiles) .CreateLogger(); services.AddSingleton <INonsensitiveAuditingFields>(new NonsensitiveAuditingFields()); return(services); }
public static IServiceCollection AddApiAuditing(this IServiceCollection services, IConfiguration configuration, IHostingEnvironment env) { var auditingConfiguration = new AuditingConfiguration(configuration); var auditRoot = auditingConfiguration.AuditingRoot; var minLevel = auditingConfiguration.MinLevel; var defaultAuditRoot = auditingConfiguration.GetDefaultAuditRoot(env); // If invalid directory was specified in the configuration. Reset to default if (!Directory.Exists(auditRoot)) { auditRoot = defaultAuditRoot; } if (!auditingConfiguration.Enabled) { // Disable auditing minLevel = (LogLevel)(1 + (int)LogEventLevel.Fatal); } AuditAttribute.Logger = new LoggerConfiguration() .MinimumLevel .Is(LoggingConfiguration.ToLogEventLevel(minLevel)) .WriteTo .RollingFile(Path.Combine(auditRoot, auditingConfiguration.FileName), retainedFileCountLimit: null) .CreateLogger(); return(services); }