Skip to content

Commit

Permalink
style: apply code style
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Jan 13, 2022
1 parent fdfa232 commit a45ed09
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<ItemGroup>
<ProjectReference Include="..\Sitko.Core.App.Blazor\Sitko.Core.App.Blazor.csproj"/>
</ItemGroup>

</Project>
193 changes: 96 additions & 97 deletions src/Sitko.Core.ElasticStack/ElasticStackModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,122 +11,121 @@
using Serilog.Sinks.Elasticsearch;
using Sitko.Core.App;

namespace Sitko.Core.ElasticStack
namespace Sitko.Core.ElasticStack;

public class ElasticStackModule : BaseApplicationModule<ElasticStackModuleOptions>,
IHostBuilderModule<ElasticStackModuleOptions>, ILoggingModule<ElasticStackModuleOptions>
{
public class ElasticStackModule : BaseApplicationModule<ElasticStackModuleOptions>,
IHostBuilderModule<ElasticStackModuleOptions>, ILoggingModule<ElasticStackModuleOptions>
public void ConfigureHostBuilder(ApplicationContext context, IHostBuilder hostBuilder,
ElasticStackModuleOptions startupOptions)
{
public void ConfigureHostBuilder(ApplicationContext context, IHostBuilder hostBuilder,
ElasticStackModuleOptions startupOptions)
if (startupOptions.ApmEnabled)
{
if (startupOptions.ApmEnabled)
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVICE_NAME", context.Name);
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVICE_VERSION", context.Version);
Environment.SetEnvironmentVariable("ELASTIC_APM_TRANSACTION_SAMPLE_RATE",
startupOptions.ApmTransactionSampleRate.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:TransactionMaxSpans",
startupOptions.ApmTransactionMaxSpans.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:CentralConfig",
startupOptions.ApmCentralConfig.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:SanitizeFieldNames",
startupOptions.ApmCentralConfig.ToString(CultureInfo.InvariantCulture));
if (startupOptions.ApmSanitizeFieldNames != null && startupOptions.ApmSanitizeFieldNames.Any())
{
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVICE_NAME", context.Name);
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVICE_VERSION", context.Version);
Environment.SetEnvironmentVariable("ELASTIC_APM_TRANSACTION_SAMPLE_RATE",
startupOptions.ApmTransactionSampleRate.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:TransactionMaxSpans",
startupOptions.ApmTransactionMaxSpans.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:CentralConfig",
startupOptions.ApmCentralConfig.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:SanitizeFieldNames",
startupOptions.ApmCentralConfig.ToString(CultureInfo.InvariantCulture));
if (startupOptions.ApmSanitizeFieldNames != null && startupOptions.ApmSanitizeFieldNames.Any())
{
Environment.SetEnvironmentVariable("ElasticApm:SanitizeFieldNames",
string.Join(", ", startupOptions.ApmSanitizeFieldNames));
}
string.Join(", ", startupOptions.ApmSanitizeFieldNames));
}

if (startupOptions.ApmGlobalLabels.Any())
{
Environment.SetEnvironmentVariable("ElasticApm:GlobalLabels",
string.Join(",", startupOptions.ApmGlobalLabels.Select(kv => $"{kv.Key}={kv.Value}")));
}
if (startupOptions.ApmGlobalLabels.Any())
{
Environment.SetEnvironmentVariable("ElasticApm:GlobalLabels",
string.Join(",", startupOptions.ApmGlobalLabels.Select(kv => $"{kv.Key}={kv.Value}")));
}

Environment.SetEnvironmentVariable("ElasticApm:ServerUrls",
string.Join(",", startupOptions.ApmServerUrls));
Environment.SetEnvironmentVariable("ElasticApm:ServerUrls",
string.Join(",", startupOptions.ApmServerUrls));

Environment.SetEnvironmentVariable("ElasticApm:SecretToken", startupOptions.ApmSecretToken);
Environment.SetEnvironmentVariable("ElasticApm:ApiKey", startupOptions.ApmApiKey);
Environment.SetEnvironmentVariable("ElasticApm:VerifyServerCert",
startupOptions.ApmVerifyServerCert.ToString());
Environment.SetEnvironmentVariable("ElasticApm:FlushInterval",
$"{TimeSpan.FromSeconds(startupOptions.ApmFlushIntervalInSeconds).TotalSeconds}s");
Environment.SetEnvironmentVariable("ElasticApm:MaxBatchEventCount",
startupOptions.ApmMaxBatchEventCount.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:MaxQueueEventCount",
startupOptions.ApmMaxQueueEventCount.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:MetricsInterval",
$"{TimeSpan.FromSeconds(startupOptions.ApmMetricsIntervalInSeconds).TotalSeconds}s");
if (startupOptions.ApmDisableMetrics != null && startupOptions.ApmDisableMetrics.Any())
{
Environment.SetEnvironmentVariable("ElasticApm:DisableMetrics",
string.Join(",", startupOptions.ApmDisableMetrics));
}

Environment.SetEnvironmentVariable("ElasticApm:CaptureBody", startupOptions.ApmCaptureBody);
if (startupOptions.ApmCaptureBodyContentTypes != null &&
startupOptions.ApmCaptureBodyContentTypes.Any())
{
Environment.SetEnvironmentVariable("ElasticApm:CaptureBodyContentTypes",
string.Join(",", startupOptions.ApmCaptureBodyContentTypes));
}
Environment.SetEnvironmentVariable("ElasticApm:SecretToken", startupOptions.ApmSecretToken);
Environment.SetEnvironmentVariable("ElasticApm:ApiKey", startupOptions.ApmApiKey);
Environment.SetEnvironmentVariable("ElasticApm:VerifyServerCert",
startupOptions.ApmVerifyServerCert.ToString());
Environment.SetEnvironmentVariable("ElasticApm:FlushInterval",
$"{TimeSpan.FromSeconds(startupOptions.ApmFlushIntervalInSeconds).TotalSeconds}s");
Environment.SetEnvironmentVariable("ElasticApm:MaxBatchEventCount",
startupOptions.ApmMaxBatchEventCount.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:MaxQueueEventCount",
startupOptions.ApmMaxQueueEventCount.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:MetricsInterval",
$"{TimeSpan.FromSeconds(startupOptions.ApmMetricsIntervalInSeconds).TotalSeconds}s");
if (startupOptions.ApmDisableMetrics != null && startupOptions.ApmDisableMetrics.Any())
{
Environment.SetEnvironmentVariable("ElasticApm:DisableMetrics",
string.Join(",", startupOptions.ApmDisableMetrics));
}

Environment.SetEnvironmentVariable("ElasticApm:CaptureHeaders",
startupOptions.ApmCaptureHeaders.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:UseElasticTraceparentHeader",
startupOptions.ApmUseElasticTraceparentHeader.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:StackTraceLimit",
startupOptions.ApmStackTraceLimit.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:SpanFramesMinDuration",
$"{TimeSpan.FromSeconds(startupOptions.ApmSpanFramesMinDurationInSeconds).TotalMilliseconds}ms");
Environment.SetEnvironmentVariable("ElasticApm:ApmLogLevel", startupOptions.ApmLogLevel);
hostBuilder.UseAllElasticApm();
Environment.SetEnvironmentVariable("ElasticApm:CaptureBody", startupOptions.ApmCaptureBody);
if (startupOptions.ApmCaptureBodyContentTypes != null &&
startupOptions.ApmCaptureBodyContentTypes.Any())
{
Environment.SetEnvironmentVariable("ElasticApm:CaptureBodyContentTypes",
string.Join(",", startupOptions.ApmCaptureBodyContentTypes));
}

Environment.SetEnvironmentVariable("ElasticApm:CaptureHeaders",
startupOptions.ApmCaptureHeaders.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:UseElasticTraceparentHeader",
startupOptions.ApmUseElasticTraceparentHeader.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:StackTraceLimit",
startupOptions.ApmStackTraceLimit.ToString(CultureInfo.InvariantCulture));
Environment.SetEnvironmentVariable("ElasticApm:SpanFramesMinDuration",
$"{TimeSpan.FromSeconds(startupOptions.ApmSpanFramesMinDurationInSeconds).TotalMilliseconds}ms");
Environment.SetEnvironmentVariable("ElasticApm:ApmLogLevel", startupOptions.ApmLogLevel);
hostBuilder.UseAllElasticApm();
}
}

public override string OptionsKey => "ElasticApm";

public void ConfigureLogging(ApplicationContext context, ElasticStackModuleOptions options,
LoggerConfiguration loggerConfiguration)
public void ConfigureLogging(ApplicationContext context, ElasticStackModuleOptions options,
LoggerConfiguration loggerConfiguration)
{
if (options.LoggingEnabled)
{
if (options.LoggingEnabled)
var rolloverAlias = string.IsNullOrEmpty(options.LoggingLiferRolloverAlias)
? $"dotnet-logs-{context.Name.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}-{context.Environment.EnvironmentName.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}"
: options.LoggingLiferRolloverAlias;
var sinkOptions = new ElasticsearchSinkOptions(options.ElasticSearchUrls)
{
var rolloverAlias = string.IsNullOrEmpty(options.LoggingLiferRolloverAlias)
? $"dotnet-logs-{context.Name.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}-{context.Environment.EnvironmentName.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}"
: options.LoggingLiferRolloverAlias;
var sinkOptions = new ElasticsearchSinkOptions(options.ElasticSearchUrls)
{
CustomFormatter = new EcsTextFormatter(),
AutoRegisterTemplate = true,
AutoRegisterTemplateVersion = options.LoggingTemplateVersion ?? AutoRegisterTemplateVersion.ESv7,
NumberOfReplicas = options.LoggingNumberOfReplicas,
IndexFormat =
options.LoggingIndexFormat ??
$"dotnet-logs-{context.Name.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}-{context.Name.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}",
TemplateName = rolloverAlias
};
CustomFormatter = new EcsTextFormatter(),
AutoRegisterTemplate = true,
AutoRegisterTemplateVersion = options.LoggingTemplateVersion ?? AutoRegisterTemplateVersion.ESv7,
NumberOfReplicas = options.LoggingNumberOfReplicas,
IndexFormat =
options.LoggingIndexFormat ??
$"dotnet-logs-{context.Name.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}-{context.Name.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}",
TemplateName = rolloverAlias
};

if (!string.IsNullOrEmpty(options.LoggingLifeCycleName))
if (!string.IsNullOrEmpty(options.LoggingLifeCycleName))
{
sinkOptions.TemplateCustomSettings = new Dictionary<string, string>
{
sinkOptions.TemplateCustomSettings = new Dictionary<string, string>
{
{ "lifecycle.name", options.LoggingLifeCycleName },
{ "lifecycle.rollover_alias", rolloverAlias }
};
sinkOptions.IndexAliases = new[] { rolloverAlias };
}

loggerConfiguration.Enrich.WithElasticApmCorrelationInfo()
.WriteTo.Elasticsearch(sinkOptions)
.Enrich.WithProperty("ApplicationName", context.Name)
.Enrich.WithProperty("ApplicationVersion", context.Version);
{ "lifecycle.name", options.LoggingLifeCycleName },
{ "lifecycle.rollover_alias", rolloverAlias }
};
sinkOptions.IndexAliases = new[] { rolloverAlias };
}

if (options.ApmEnabled)
{
loggerConfiguration.MinimumLevel.Override("Elastic.Apm", LogEventLevel.Error);
}
loggerConfiguration.Enrich.WithElasticApmCorrelationInfo()
.WriteTo.Elasticsearch(sinkOptions)
.Enrich.WithProperty("ApplicationName", context.Name)
.Enrich.WithProperty("ApplicationVersion", context.Version);
}

public override string OptionsKey => "ElasticApm";
if (options.ApmEnabled)
{
loggerConfiguration.MinimumLevel.Override("Elastic.Apm", LogEventLevel.Error);
}
}
}

0 comments on commit a45ed09

Please sign in to comment.