Skip to content

Commit

Permalink
Configure EventLogLoggerProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Jul 26, 2024
1 parent 0139253 commit abdae2c
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;
Expand All @@ -18,6 +19,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;

namespace ClassifiedAds.Infrastructure.Logging;
Expand Down Expand Up @@ -125,12 +127,12 @@ public static IWebHostBuilder UseClassifiedAdsLogger(this IWebHostBuilder builde

LoggingOptions options = SetDefault(logOptions(context.Configuration));

if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
if (options.EventLog.IsEnabled && OperatingSystem.IsWindows())
{
logging.AddEventLog(new EventLogSettings
logging.AddEventLog(configure =>
{
LogName = options.EventLog.LogName,
SourceName = options.EventLog.SourceName,
configure.LogName = options.EventLog.LogName;
configure.SourceName = options.EventLog.SourceName;
});
}

Expand Down Expand Up @@ -190,13 +192,34 @@ public static IHostBuilder UseClassifiedAdsLogger(this IHostBuilder builder, Fun

LoggingOptions options = SetDefault(logOptions(context.Configuration));

if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
if (OperatingSystem.IsWindows())
{
logging.AddEventLog(new EventLogSettings
if (options.EventLog.IsEnabled)
{
LogName = options.EventLog.LogName,
SourceName = options.EventLog.SourceName,
});
logging.AddEventLog(configure =>
{
configure.LogName = options.EventLog.LogName;
configure.SourceName = options.EventLog.SourceName;
});

logging.Services.Configure<LoggerFilterOptions>(options =>
{
// remove the logging.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Warning); added by calling Host.CreateDefaultBuilder(args)
// remember to review this in any future update
var toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.EventLog.EventLogLoggerProvider");

if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
}
else
{
// remove the EventLogLoggerProvider added by calling Host.CreateDefaultBuilder(args) or UseWindowsService()
// remember to review this in any future update
logging.RemoveEventLog();
}
}

if (options?.ApplicationInsights?.IsEnabled ?? false)
Expand Down Expand Up @@ -315,4 +338,19 @@ private static LogEventLevel ConvertToSerilogLevel(LogLevel logLevel)
_ => LogEventLevel.Fatal
};
}

public static ILoggingBuilder RemoveEventLog(this ILoggingBuilder builder)
{
var providers = builder.Services.Where(x => x.ServiceType == typeof(ILoggerProvider)).ToList();

foreach (var provider in providers)
{
if (provider.ImplementationType == typeof(EventLogLoggerProvider))
{
builder.Services.Remove(provider);
}
}

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;
Expand All @@ -18,6 +19,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;

namespace ClassifiedAds.Infrastructure.Logging;
Expand Down Expand Up @@ -125,12 +127,12 @@ public static IWebHostBuilder UseClassifiedAdsLogger(this IWebHostBuilder builde

LoggingOptions options = SetDefault(logOptions(context.Configuration));

if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
if (options.EventLog.IsEnabled && OperatingSystem.IsWindows())
{
logging.AddEventLog(new EventLogSettings
logging.AddEventLog(configure =>
{
LogName = options.EventLog.LogName,
SourceName = options.EventLog.SourceName,
configure.LogName = options.EventLog.LogName;
configure.SourceName = options.EventLog.SourceName;
});
}

Expand Down Expand Up @@ -190,13 +192,34 @@ public static IHostBuilder UseClassifiedAdsLogger(this IHostBuilder builder, Fun

LoggingOptions options = SetDefault(logOptions(context.Configuration));

if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
if (OperatingSystem.IsWindows())
{
logging.AddEventLog(new EventLogSettings
if (options.EventLog.IsEnabled)
{
LogName = options.EventLog.LogName,
SourceName = options.EventLog.SourceName,
});
logging.AddEventLog(configure =>
{
configure.LogName = options.EventLog.LogName;
configure.SourceName = options.EventLog.SourceName;
});

logging.Services.Configure<LoggerFilterOptions>(options =>
{
// remove the logging.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Warning); added by calling Host.CreateDefaultBuilder(args)
// remember to review this in any future update
var toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.EventLog.EventLogLoggerProvider");

if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
}
else
{
// remove the EventLogLoggerProvider added by calling Host.CreateDefaultBuilder(args) or UseWindowsService()
// remember to review this in any future update
logging.RemoveEventLog();
}
}

if (options?.ApplicationInsights?.IsEnabled ?? false)
Expand Down Expand Up @@ -315,4 +338,19 @@ private static LogEventLevel ConvertToSerilogLevel(LogLevel logLevel)
_ => LogEventLevel.Fatal
};
}

public static ILoggingBuilder RemoveEventLog(this ILoggingBuilder builder)
{
var providers = builder.Services.Where(x => x.ServiceType == typeof(ILoggerProvider)).ToList();

foreach (var provider in providers)
{
if (provider.ImplementationType == typeof(EventLogLoggerProvider))
{
builder.Services.Remove(provider);
}
}

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;
Expand All @@ -18,6 +19,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;

namespace ClassifiedAds.Infrastructure.Logging;
Expand Down Expand Up @@ -125,12 +127,12 @@ public static IWebHostBuilder UseClassifiedAdsLogger(this IWebHostBuilder builde

LoggingOptions options = SetDefault(logOptions(context.Configuration));

if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
if (options.EventLog.IsEnabled && OperatingSystem.IsWindows())
{
logging.AddEventLog(new EventLogSettings
logging.AddEventLog(configure =>
{
LogName = options.EventLog.LogName,
SourceName = options.EventLog.SourceName,
configure.LogName = options.EventLog.LogName;
configure.SourceName = options.EventLog.SourceName;
});
}

Expand Down Expand Up @@ -190,13 +192,34 @@ public static IHostBuilder UseClassifiedAdsLogger(this IHostBuilder builder, Fun

LoggingOptions options = SetDefault(logOptions(context.Configuration));

if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
if (OperatingSystem.IsWindows())
{
logging.AddEventLog(new EventLogSettings
if (options.EventLog.IsEnabled)
{
LogName = options.EventLog.LogName,
SourceName = options.EventLog.SourceName,
});
logging.AddEventLog(configure =>
{
configure.LogName = options.EventLog.LogName;
configure.SourceName = options.EventLog.SourceName;
});

logging.Services.Configure<LoggerFilterOptions>(options =>
{
// remove the logging.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Warning); added by calling Host.CreateDefaultBuilder(args)
// remember to review this in any future update
var toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.EventLog.EventLogLoggerProvider");

if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
}
else
{
// remove the EventLogLoggerProvider added by calling Host.CreateDefaultBuilder(args) or UseWindowsService()
// remember to review this in any future update
logging.RemoveEventLog();
}
}

if (options?.ApplicationInsights?.IsEnabled ?? false)
Expand Down Expand Up @@ -315,4 +338,19 @@ private static LogEventLevel ConvertToSerilogLevel(LogLevel logLevel)
_ => LogEventLevel.Fatal
};
}

public static ILoggingBuilder RemoveEventLog(this ILoggingBuilder builder)
{
var providers = builder.Services.Where(x => x.ServiceType == typeof(ILoggerProvider)).ToList();

foreach (var provider in providers)
{
if (provider.ImplementationType == typeof(EventLogLoggerProvider))
{
builder.Services.Remove(provider);
}
}

return builder;
}
}

0 comments on commit abdae2c

Please sign in to comment.