Created
July 20, 2017 16:04
-
-
Save ritasker/a9caec70602e657d77667ddbe5954403 to your computer and use it in GitHub Desktop.
Setup of App.Metrics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace VQConsole | |
{ | |
using System; | |
using System.IO; | |
using System.Threading; | |
using App.Metrics; | |
using App.Metrics.Extensions.Reporting.Http; | |
using App.Metrics.Extensions.Reporting.Http.Client; | |
using App.Metrics.Formatting.Ascii; | |
using App.Metrics.Reporting.Abstractions; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
using Serilog; | |
using Serilog.Debugging; | |
using Serilog.Events; | |
using Serilog.Sinks.Fluentd.Core; | |
using VQCore.Configuration; | |
using VQCore.Database; | |
using VQCore.Logging; | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var config = ConsoleRunner.BuildConfiguration(); | |
var serviceCollection = new ServiceCollection(); | |
ConfigureServices(serviceCollection); | |
ConfigureHealthChecks(serviceCollection); | |
SetupLogging(config.VqCoreConfiguration); | |
ConsoleRunner.StartMessageProcessor(config); | |
ConsoleRunner.Run(); | |
} | |
private static void ConfigureHealthChecks(ServiceCollection servies) | |
{ | |
servies | |
.AddMetrics() | |
.AddHealthChecks(factory => | |
{ | |
factory.RegisterPingHealthCheck("google ping", "google.com", TimeSpan.FromSeconds(1)); | |
}) | |
.AddReporting(factory => | |
{ | |
factory.AddHttp( | |
new HttpReporterSettings | |
{ | |
// Set the endpoint where metrics should be posted | |
HttpSettings = new HttpSettings(new Uri("http://localhost/metrics-receive")), | |
ReportInterval = TimeSpan.FromSeconds(5), | |
HttpPolicy = new HttpPolicy | |
{ | |
// The number of http failures to cause a backoff in reporting | |
FailuresBeforeBackoff = 5, | |
// Duration to backoff if the number of http failures have been reached | |
BackoffPeriod = TimeSpan.FromSeconds(30), | |
// Timeout of the underlaying HTTP call | |
Timeout = TimeSpan.FromSeconds(3) | |
} | |
}, | |
// Formats the metrics before sending over HTTP | |
new AsciiMetricPayloadBuilder()); | |
}); | |
var provider = servies.BuildServiceProvider(); | |
var reporterFactory = provider.GetRequiredService<IReportFactory>(); | |
var reporter = reporterFactory.CreateReporter(); | |
reporter.RunReports(provider.GetRequiredService<IMetrics>(), CancellationToken.None); | |
} | |
private static void ConfigureServices(IServiceCollection services) | |
{ | |
var loggerFactory = new LoggerFactory(); | |
loggerFactory.AddConsole((l, s) => s == LogLevel.Trace); | |
services.AddSingleton<ILoggerFactory, LoggerFactory>(); | |
services.AddLogging(); | |
} | |
private static void SetupLogging(VQCoreConfiguration vqCoreConfiguration) | |
{ | |
var dbConnectionProvider = DbConnectionProviderFactory.GetDbConnectionProvider(vqCoreConfiguration.Database); | |
var logSettingsProvider = new LogSettingsProvider(dbConnectionProvider); | |
var logSettings = logSettingsProvider.GetSettings(); | |
var config = new LoggerConfiguration(); | |
var logging = config.MinimumLevel.Verbose().Enrich.WithThreadId() | |
.WriteTo.Logger(lc => | |
lc.Filter.ByIncludingOnly(GetFilter(LoggerName.Server)) | |
.WriteTo.RollingFile( | |
Path.Combine(logSettings.LogOutputDirectory, $"vqservice_{DateTime.Now:yyyy-MM-dd_HH.mm.ss.ffffff}.log"), | |
outputTemplate: "{Timestamp:yyyy-MM-dd HH\\:mm\\:ss.ffffff} ThreadId: {ThreadId} {Level} {Message} {Exception} {NewLine}", | |
fileSizeLimitBytes: logSettings.FileSize, | |
retainedFileCountLimit: logSettings.NumberOfFiles | |
)) | |
.WriteTo.Logger(lc => | |
lc.Filter.ByIncludingOnly(GetFilter(LoggerName.MessageBus)) | |
.WriteTo.RollingFile( | |
Path.Combine(logSettings.LogOutputDirectory, $"vqservice-MsgBus_{DateTime.Now:yyyy-MM-dd_HH.mm.ss.ffffff}.log"), | |
outputTemplate: "{Timestamp:yyyy-MM-dd HH\\:mm\\:ss.ffffff} ThreadId: {ThreadId} {Level} {Message} {Exception} {NewLine}", | |
fileSizeLimitBytes: logSettings.FileSize, | |
retainedFileCountLimit: logSettings.NumberOfFiles | |
)); | |
if (vqCoreConfiguration.EnabledLogging.Fluentd) | |
{ | |
logging.WriteTo.Fluentd(new FluentdHandlerSettings | |
{ | |
Tag = vqCoreConfiguration.Fluentd.Tag, | |
Host = vqCoreConfiguration.Fluentd.Hostname, | |
Port = vqCoreConfiguration.Fluentd.Port | |
}); | |
} | |
Log.Logger = logging.CreateLogger(); | |
SelfLog.Enable(Console.WriteLine); | |
Console.WriteLine("Logging setup complete"); | |
} | |
private static Func<LogEvent, bool> GetFilter(string loggerName) | |
{ | |
loggerName = "\"" + loggerName + "\""; | |
return (evt) => evt.Properties.ContainsKey(nameof(LoggerName)) && evt.Properties[nameof(LoggerName)].ToString() == loggerName; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment