Skip to content

Commit

Permalink
remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Jun 13, 2024
1 parent 1b9d588 commit 7477ac8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
},
"File": {
"MinimumLogEventLevel": "Debug"
},
"Elasticsearch": {
"IsEnabled": false,
"Host": "http://localhost:9200",
"IndexFormat": "classifiedads",
"MinimumLogEventLevel": "Debug"
}
},
"Caching": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.3.0" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="9.0.3" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
using Microsoft.Extensions.Logging.EventLog;
using Serilog;
using Serilog.Exceptions;
using Serilog.Formatting.Json;
using Serilog.Sinks.Elasticsearch;
using Serilog.Sinks.File;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -29,9 +26,9 @@ private static void UseClassifiedAdsLogger(this IWebHostEnvironment env, Logging
loggerConfiguration = loggerConfiguration
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.Enrich.With<ActivityEnricher>()
.Enrich.WithMachineName()
.Enrich.WithEnvironmentUserName()
.Enrich.WithProperty("ProcessId", Environment.ProcessId)
.Enrich.WithProperty("Assembly", assemblyName)
.Enrich.WithProperty("Application", env.ApplicationName)
.Enrich.WithProperty("EnvironmentName", env.EnvironmentName)
Expand All @@ -40,8 +37,7 @@ private static void UseClassifiedAdsLogger(this IWebHostEnvironment env, Logging
.Enrich.WithExceptionDetails()
.Filter.ByIncludingOnly((logEvent) =>
{
if (logEvent.Level >= options.File.MinimumLogEventLevel
|| logEvent.Level >= options.Elasticsearch.MinimumLogEventLevel)
if (logEvent.Level >= options.File.MinimumLogEventLevel)
{
var sourceContext = logEvent.Properties.ContainsKey("SourceContext")
? logEvent.Properties["SourceContext"].ToString()
Expand All @@ -59,25 +55,9 @@ private static void UseClassifiedAdsLogger(this IWebHostEnvironment env, Logging
rollOnFileSizeLimit: true,
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(1),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] [TraceId: {TraceId}] {Message:lj}{NewLine}{Exception}",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] [TraceId: {TraceId}] [MachineName: {MachineName}] [ProcessId: {ProcessId}] {Message:lj}{NewLine}{Exception}",
restrictedToMinimumLevel: options.File.MinimumLogEventLevel);

if (options.Elasticsearch != null && options.Elasticsearch.IsEnabled)
{
loggerConfiguration = loggerConfiguration
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(options.Elasticsearch.Host))
{
MinimumLogEventLevel = options.Elasticsearch.MinimumLogEventLevel,
AutoRegisterTemplate = true,
AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
IndexFormat = options.Elasticsearch.IndexFormat + "-{0:yyyy.MM.dd}",
// BufferBaseFilename = Path.Combine(env.ContentRootPath, "logs", "buffer"),
InlineFields = true,
EmitEventFailure = EmitEventFailureHandling.WriteToFailureSink,
FailureSink = new FileSink(Path.Combine(logsPath, "elasticsearch-failures.txt"), new JsonFormatter(), null),
});
}

Log.Logger = loggerConfiguration.CreateLogger();
}

Expand All @@ -99,12 +79,6 @@ private static LoggingOptions SetDefault(LoggingOptions options)
MinimumLogEventLevel = Serilog.Events.LogEventLevel.Warning,
};

options.Elasticsearch ??= new ElasticsearchOptions
{
IsEnabled = false,
MinimumLogEventLevel = Serilog.Events.LogEventLevel.Warning,
};

options.EventLog ??= new EventLogOptions
{
IsEnabled = false,
Expand Down Expand Up @@ -141,7 +115,7 @@ public static IWebHostBuilder UseClassifiedAdsLogger(this IWebHostBuilder builde

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

if (options.EventLog != null && options.EventLog.IsEnabled)
if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
{
logging.AddEventLog(new EventLogSettings
{
Expand All @@ -152,7 +126,12 @@ public static IWebHostBuilder UseClassifiedAdsLogger(this IWebHostBuilder builde

if (options?.ApplicationInsights?.IsEnabled ?? false)
{
logging.AddApplicationInsights(options.ApplicationInsights.InstrumentationKey);
logging.AddApplicationInsights(
configureTelemetryConfiguration: (config) =>
{
config.ConnectionString = options.ApplicationInsights.InstrumentationKey;
},
configureApplicationInsightsLoggerOptions: (options) => { });
}

context.HostingEnvironment.UseClassifiedAdsLogger(options);
Expand All @@ -176,7 +155,7 @@ public static IHostBuilder UseClassifiedAdsLogger(this IHostBuilder builder, Fun

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

if (options.EventLog != null && options.EventLog.IsEnabled)
if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
{
logging.AddEventLog(new EventLogSettings
{
Expand All @@ -187,7 +166,12 @@ public static IHostBuilder UseClassifiedAdsLogger(this IHostBuilder builder, Fun

if (options?.ApplicationInsights?.IsEnabled ?? false)
{
logging.AddApplicationInsights(options.ApplicationInsights.InstrumentationKey);
logging.AddApplicationInsights(
configureTelemetryConfiguration: (config) =>
{
config.ConnectionString = options.ApplicationInsights.InstrumentationKey;
},
configureApplicationInsightsLoggerOptions: (options) => { });
}

context.HostingEnvironment.UseClassifiedAdsLogger(options);
Expand All @@ -207,18 +191,17 @@ private static void UseClassifiedAdsLogger(this IHostEnvironment env, LoggingOpt
loggerConfiguration = loggerConfiguration
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.Enrich.With<ActivityEnricher>()
.Enrich.WithMachineName()
.Enrich.WithEnvironmentUserName()
.Enrich.WithProperty("ProcessId", Environment.ProcessId)
.Enrich.WithProperty("Assembly", assemblyName)
.Enrich.WithProperty("Application", env.ApplicationName)
.Enrich.WithProperty("EnvironmentName", env.EnvironmentName)
.Enrich.WithProperty("ContentRootPath", env.ContentRootPath)
.Enrich.WithExceptionDetails()
.Filter.ByIncludingOnly((logEvent) =>
{
if (logEvent.Level >= options.File.MinimumLogEventLevel
|| logEvent.Level >= options.Elasticsearch.MinimumLogEventLevel)
if (logEvent.Level >= options.File.MinimumLogEventLevel)
{
var sourceContext = logEvent.Properties.ContainsKey("SourceContext")
? logEvent.Properties["SourceContext"].ToString()
Expand All @@ -236,25 +219,9 @@ private static void UseClassifiedAdsLogger(this IHostEnvironment env, LoggingOpt
rollOnFileSizeLimit: true,
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(1),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] [TraceId: {TraceId}] {Message:lj}{NewLine}{Exception}",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] [TraceId: {TraceId}] [MachineName: {MachineName}] [ProcessId: {ProcessId}] {Message:lj}{NewLine}{Exception}",
restrictedToMinimumLevel: options.File.MinimumLogEventLevel);

if (options.Elasticsearch != null && options.Elasticsearch.IsEnabled)
{
loggerConfiguration = loggerConfiguration
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(options.Elasticsearch.Host))
{
MinimumLogEventLevel = options.Elasticsearch.MinimumLogEventLevel,
AutoRegisterTemplate = true,
AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
IndexFormat = options.Elasticsearch.IndexFormat + "-{0:yyyy.MM.dd}",
// BufferBaseFilename = Path.Combine(env.ContentRootPath, "logs", "buffer"),
InlineFields = true,
EmitEventFailure = EmitEventFailureHandling.WriteToFailureSink,
FailureSink = new FileSink(Path.Combine(logsPath, "elasticsearch-failures.txt"), new JsonFormatter(), null),
});
}

Log.Logger = loggerConfiguration.CreateLogger();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class LoggingOptions

public FileOptions File { get; set; }

public ElasticsearchOptions Elasticsearch { get; set; }

public EventLogOptions EventLog { get; set; }

public ApplicationInsightsOptions ApplicationInsights { get; set; }
Expand Down

0 comments on commit 7477ac8

Please sign in to comment.