static void TestRollingEventSequence( IEnumerable<LogEvent> events, int? retainedFiles, Action<IList<string>> verifyWritten) { var fileName = Some.String() + "-{Date}.txt"; var folder = Some.TempFolderPath(); var pathFormat = Path.Combine(folder, fileName); var log = new LoggerConfiguration() .WriteTo.RollingFile(pathFormat, retainedFileCountLimit: retainedFiles) .CreateLogger(); var verified = new List<string>(); try { foreach (var @event in events) { Clock.SetTestDateTimeNow(@event.Timestamp.DateTime); log.Write(@event); var expected = pathFormat.Replace("{Date}", @event.Timestamp.ToString("yyyyMMdd")); Assert.That(File.Exists(expected)); verified.Add(expected); } } finally { ((IDisposable)log).Dispose(); verifyWritten(verified); Directory.Delete(folder, true); } }
public void CanUseCustomSettingDelimiterToConfigureSettings() { const string prefix1 = "|"; const string prefix2 = "!"; // Make sure we have the expected keys in the App.config Assert.AreEqual("Warning", ConfigurationManager.AppSettings["serilog|minimum-level"]); Assert.AreEqual("Error", ConfigurationManager.AppSettings["serilog!minimum-level"]); var log1 = new LoggerConfiguration() .WriteTo.Observers(o => { }) .ReadFrom.AppSettings(null,prefix1) .CreateLogger(); var log2 = new LoggerConfiguration() .WriteTo.Observers(o => { }) .ReadFrom.AppSettings(null,prefix2) .CreateLogger(); Assert.IsFalse(log1.IsEnabled(LogEventLevel.Information)); Assert.IsTrue(log1.IsEnabled(LogEventLevel.Warning)); Assert.IsFalse(log2.IsEnabled(LogEventLevel.Warning)); Assert.IsTrue(log2.IsEnabled(LogEventLevel.Error)); }
internal LoggerSinkConfiguration(LoggerConfiguration loggerConfiguration, Action<ILogEventSink> addSink) { if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration"); if (addSink == null) throw new ArgumentNullException("addSink"); _loggerConfiguration = loggerConfiguration; _addSink = addSink; }
public void UsingSuperLongLogMessageWorks() { var charcounts = new[] { 10*1000, 20*1000, 30*1000, 40*1000, 70*1000 }; foreach (var charcount in charcounts) { var log = new LoggerConfiguration() .WriteTo.EventLog("EventLogSinkTests") .CreateLogger(); var guid = Guid.NewGuid().ToString("D"); log.Information("This is a super long message which might be trimmed. Guid is {Guid}.The following text has {charcount} chars: {LongText}" , guid , charcount , new string('x', charcount)); Assert.IsTrue(EventLogMessageWithSpecificBodyExists(guid), "The message was not found in the eventlog. Charcount was " + charcount); } }
public void MoreNestedPropertiesOverrideLessNestedOnes() { LogEvent lastEvent = null; var log = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Sink(new DelegatingSink(e => lastEvent = e)) .CreateLogger(); using (LogContext.PushProperty("A", 1)) { log.Write(Some.InformationEvent()); Assert.AreEqual(1, lastEvent.Properties["A"].LiteralValue()); using (LogContext.PushProperty("A", 2)) { log.Write(Some.InformationEvent()); Assert.AreEqual(2, lastEvent.Properties["A"].LiteralValue()); } log.Write(Some.InformationEvent()); Assert.AreEqual(1, lastEvent.Properties["A"].LiteralValue()); } log.Write(Some.InformationEvent()); Assert.IsFalse(lastEvent.Properties.ContainsKey("A")); }
public async Task ContextPropertiesCrossAsyncCalls() { LogEvent lastEvent = null; var log = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Sink(new DelegatingSink(e => lastEvent = e)) .CreateLogger(); using (LogContext.PushProperty("A", 1)) { var pre = Thread.CurrentThread.ManagedThreadId; await Task.Delay(1000); var post = Thread.CurrentThread.ManagedThreadId; log.Write(Some.InformationEvent()); Assert.AreEqual(1, lastEvent.Properties["A"].LiteralValue()); // No problem if this happens occasionally. if (pre == post) Assert.Inconclusive("The test was marshalled back to the same thread after awaiting"); } }
public void LoggingLevelSwitchDynamicallyChangesLevel() { var events = new List<LogEvent>(); var sink = new DelegatingSink(events.Add); var levelSwitch = new LoggingLevelSwitch(LogEventLevel.Information); var log = new LoggerConfiguration() .MinimumLevel.ControlledBy(levelSwitch) .WriteTo.Sink(sink) .CreateLogger() .ForContext<LoggerTests>(); log.Debug("Suppressed"); log.Information("Emitted"); log.Warning("Emitted"); // Change the level levelSwitch.MinimumLevel = LogEventLevel.Error; log.Warning("Suppressed"); log.Error("Emitted"); log.Fatal("Emitted"); Assert.Equal(4, events.Count); Assert.True(events.All(evt => evt.RenderMessage() == "Emitted")); }
public void AttributesAreConsultedWhenDestructuring() { LogEvent evt = null; var log = new LoggerConfiguration() .Destructure.UsingAttributes() .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); var customized = new Customized { ImmutableScalar = new ImmutableScalar(), MutableScalar = new MutableScalar(), NotAScalar = new NotAScalar(), Ignored = "Hello, there", ScalarAnyway = new NotAScalar() }; log.Information("Here is {@Customized}", customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); Assert.IsInstanceOf<ImmutableScalar>(props["ImmutableScalar"].LiteralValue()); Assert.AreEqual(new MutableScalar().ToString(), props["MutableScalar"].LiteralValue()); Assert.IsInstanceOf<StructureValue>(props["NotAScalar"]); Assert.IsFalse(props.ContainsKey("Ignored")); Assert.IsInstanceOf<NotAScalar>(props["ScalarAnyway"].LiteralValue()); }
public void WhenALoggerWritesToTheSinkItIsRetrievableFromTheTableWithProperties() { var storageAccount = CloudStorageAccount.DevelopmentStorageAccount; var tableClient = storageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference("LogEventEntity"); table.DeleteIfExists(); var logger = new LoggerConfiguration() .WriteTo.AzureTableStorageWithProperties(storageAccount) .CreateLogger(); var exception = new ArgumentException("Some exception"); const string messageTemplate = "{Properties} should go in their {Numbered} {Space}"; logger.Information(exception, messageTemplate, "Properties", 1234, ' '); var result = table.ExecuteQuery(new TableQuery().Take(1)).First(); // Check the presence of same properties as in previous version Assert.AreEqual(messageTemplate, result.Properties["MessageTemplate"].StringValue); Assert.AreEqual("Information", result.Properties["Level"].StringValue); Assert.AreEqual("System.ArgumentException: Some exception", result.Properties["Exception"].StringValue); Assert.AreEqual("\"Properties\" should go in their 1234 ", result.Properties["RenderedMessage"].StringValue); // Check the presence of the new properties. Assert.AreEqual("Properties", result.Properties["Properties"].PropertyAsObject); Assert.AreEqual(1234, result.Properties["Numbered"].PropertyAsObject); Assert.AreEqual(" ", result.Properties["Space"].PropertyAsObject); }
async protected override Task RunAsync(CancellationToken cancellationToken) { var esLogsConfig = new FabricConfigProvider<ElasticSearchOutputConfig>("ElasticSearchLogs").Config; var esMetricsConfig = new FabricConfigProvider<ElasticSearchOutputConfig>("ElasticSearchMetrics").Config; var logger = new LoggerConfiguration() .ConfigureMOUSETypesDestructure() .MinimumLevel.Warning() .Enrich.With(new AzureServiceFabricSerilogEnricher(Context)) .Enrich.With<ExceptionEnricher>() .WriteTo.Elasticsearch( new ElasticsearchSinkOptions(new Uri(esLogsConfig.ElasticSearchUri)) { IndexFormat = $"{esLogsConfig.ElasticSearchIndexName}-{{0:yyyy.MM.dd}}" }) .CreateLogger(); Log.Logger = logger; Metric.Config.WithAllCounters(); //Metric.PerformanceCounter("Actor calls waiting for actor lock", "Service Fabric Actor", "# of actor calls waiting for actor lock", "*", Unit.Items); //Metric.PerformanceCounter("Actor outstanding requests", "Service Fabric Actor", "# of outstanding requests", "*", Unit.Items); //Metric.PerformanceCounter("Actor Average milliseconds actor lock held", "Service Fabric Actor", "Average milliseconds actor lock held", "*", Unit.Custom("ms")); //Metric.PerformanceCounter("Actor Average milliseconds for request deserialization", "Service Fabric Actor", "Average milliseconds for request deserialization", "*", Unit.Custom("ms")); //Metric.PerformanceCounter("Actor Average milliseconds for response serialization", "Service Fabric Actor", "Average milliseconds for response serialization", "*", Unit.Custom("ms")); //Metric.PerformanceCounter("Actor Average milliseconds per load state operation", "Service Fabric Actor", "Average milliseconds per load state operation", "*", Unit.Custom("ms")); //Metric.PerformanceCounter("Actor Average milliseconds per lock wait", "Service Fabric Actor", "Average milliseconds per lock wait", "*", Unit.Custom("ms")); //Metric.PerformanceCounter("Actor Average milliseconds per request", "Service Fabric Actor", "Average milliseconds per request", "*", Unit.Custom("ms")); //Metric.PerformanceCounter("Actor Average milliseconds per save state operation", "Service Fabric Actor", "Average milliseconds per save state operation", "*", Unit.Custom("ms")); //Metric.PerformanceCounter("Actor Average OnActivateAsync milliseconds", "Service Fabric Actor", "Average OnActivateAsync milliseconds", "*", Unit.Custom("ms")); //Metric.PerformanceCounter("Actor Method Average milliseconds per invocation", "Service Fabric Actor Method", "Average milliseconds per invocation", "*", Unit.Custom("ms")); //Metric.PerformanceCounter("Actor Method Exceptions thrown/Sec", "Service Fabric Actor Method", "Exceptions thrown/Sec", "*", Unit.Custom("rate/sec")); //Metric.PerformanceCounter("Actor Method Invocations/Sec", "Service Fabric Actor Method", "Invocations/Sec", "*", Unit.Custom("rate/sec")); var metricsSubscription = new TelemetryPipe() .CollectMetricsNet(5, ServiceFabricHelpers.GetEnvironmentProperties(Context), true) .SendToElasticSearch(esMetricsConfig) .Start(); var logsSubscription = new TelemetryPipe() .CollectEventSourceEvents(new[] { //new ETWProvider("Microsoft-ServiceFabric-Services"), //new ETWProvider("Microsoft-ServiceFabric-Actors"), new ETWProvider("SFActorsPerfTest-SFTestActor") }, ServiceFabricHelpers.GetEnvironmentProperties(Context)) .SendToElasticSearch(esLogsConfig) .Start(); using (logsSubscription) using (metricsSubscription) { await base.RunAsync(cancellationToken); await Task.Delay(Timeout.Infinite, cancellationToken); } }
static void TestRollingEventSequence(IEnumerable<LogEvent> events, int? retainedFiles, Action<IList<string>> verifyWritten) { var fileName = Some.String() + "-{Date}.txt"; var folder = Some.TempFolderPath(); var pathFormat = Path.Combine(folder, fileName); var log = new LoggerConfiguration() .WriteTo.SizeRollingFile(pathFormat, retainedFileDurationLimit: TimeSpan.FromSeconds(180)) .CreateLogger(); var verified = new List<string>(); try { foreach (var @event in events) { log.Write(@event); var expected = pathFormat.Replace("{Date}", DateTime.UtcNow.ToString("yyyyMMdd") + "_0001"); Assert.True(File.Exists(expected)); verified.Add(expected); } } finally { ((IDisposable)log).Dispose(); verifyWritten(verified); Directory.Delete(folder, true); } }
static void TestLoggingAndDelete(string path) { ILogger log = null; try { log = new LoggerConfiguration() .WriteTo.File(path) .CreateLogger(); var message = Some.MessageTemplate(); log.Write(new LogEvent( DateTimeOffset.Now, LogEventLevel.Information, null, message, Enumerable.Empty<LogEventProperty>())); var refile = System.IO.File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var content = new StreamReader(refile).ReadToEnd(); refile.Dispose(); Assert.That(content.Contains(message.Text)); } finally { var disposable = (IDisposable) log; if (disposable != null) disposable.Dispose(); System.IO.File.Delete(path); } }
internal LoggerMinimumLevelConfiguration(LoggerConfiguration loggerConfiguration, Action<LogEventLevel> setMinimum) { if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration"); if (setMinimum == null) throw new ArgumentNullException("setMinimum"); _loggerConfiguration = loggerConfiguration; _setMinimum = setMinimum; }
static void TestRollingEventSequence(params LogEvent[] events) { var fileName = Some.String() + "{0}.txt"; var folder = Some.TempFolderPath(); var pathFormat = Path.Combine(folder, fileName); var log = new LoggerConfiguration() .WriteTo.RollingFile(pathFormat) .CreateLogger(); try { foreach (var @event in events) { log.Write(@event); var expected = string.Format(pathFormat, @event.Timestamp.ToString("yyyy-MM-dd")); Assert.That(System.IO.File.Exists(expected)); } } finally { ((IDisposable)log).Dispose(); Directory.Delete(folder, true); } }
/// <summary> /// Write log events to a sub-logger, where further processing may occur. Events through /// the sub-logger will be constrained by filters and enriched by enrichers that are /// active in the parent. A sub-logger cannot be used to log at a more verbose level, but /// a less verbose level is possible. /// </summary> /// <param name="configureLogger">An action that configures the sub-logger.</param> /// <returns>Configuration object allowing method chaining.</returns> public LoggerConfiguration Logger( Action<LoggerConfiguration> configureLogger) { if (configureLogger == null) throw new ArgumentNullException("configureLogger"); var lc = new LoggerConfiguration(); configureLogger(lc); return Sink(new CopyingSink((ILogEventSink)lc.CreateLogger())); }
public void LoggerTest() { var logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.WebRequest(new Uri(Url)) .CreateLogger(); logger.Information("Hello World"); }
internal LoggerFilterConfiguration( LoggerConfiguration loggerConfiguration, Action<ILogEventFilter> addFilter) { if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration"); if (addFilter == null) throw new ArgumentNullException("addFilter"); _loggerConfiguration = loggerConfiguration; _addFilter = addFilter; }
public void ShouldCreateInstanceOfLogger() { var loggerConfiguration = new LoggerConfiguration(); loggerConfiguration.WriteTo.SizeRollingFile(new CompactJsonFormatter(), "c:\\logs\\Log-{{Date}}.json", retainedFileDurationLimit: TimeSpan.FromDays(3)); var logger = loggerConfiguration.CreateLogger(); Assert.IsNotNull(logger); }
public static LogEvent GetLogEvent(Action<ILogger> writeAction) { LogEvent result = null; var l = new LoggerConfiguration() .WriteTo.Sink(new DelegatingSink(le => result = le)) .CreateLogger(); writeAction(l); return result; }
private static void Main(string[] args) { var log = new LoggerConfiguration() .WriteTo.ColoredConsole() .CreateLogger(); Log.Logger = log; Foo.Bar(); Console.ReadLine(); }
public void UsingSpecialCharsWorks() { var log = new LoggerConfiguration() .WriteTo.EventLog("EventLogSinkTests") .CreateLogger(); var guid = Guid.NewGuid().ToString("D"); log.Information("This is a mesage with a {Guid} and a special char {char}", guid, "%1"); Assert.IsTrue(EventLogMessageWithSpecificBodyExists(guid), "The message was not found in the eventlog."); }
private static ILogger CreateLoggerThatCrashes() { var loggerConfig = new LoggerConfiguration() .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:31234")) { AutoRegisterTemplate = true, TemplateName = "crash" }); return loggerConfig.CreateLogger(); }
private static void Main(string[] args) { var log = new LoggerConfiguration() .WriteTo.ColoredConsole(outputTemplate: "{Timestamp:HH:mm} [{Level}] ({Name:l}) {Message}{NewLine}{Exception}") .CreateLogger(); Log.Logger = log; Foo.Bar(); Console.ReadLine(); }
static void Main() { const int iterations = 1000000; var log = new LoggerConfiguration() .WriteTo.File("test-" + Guid.NewGuid() + ".log") .CreateLogger(); for (var i = 0; i < iterations; i++) { log.Information("Running iteration {I:00.0} for {@J}!", i, new { Goal = "Speed" }); } }
internal LoggerMinimumLevelConfiguration(LoggerConfiguration loggerConfiguration, Action<LogEventLevel> setMinimum, Action<LoggingLevelSwitch> setLevelSwitch, Action<string, LoggingLevelSwitch> addOverride) { if (loggerConfiguration == null) throw new ArgumentNullException(nameof(loggerConfiguration)); if (setMinimum == null) throw new ArgumentNullException(nameof(setMinimum)); if (addOverride == null) throw new ArgumentNullException(nameof(addOverride)); _loggerConfiguration = loggerConfiguration; _setMinimum = setMinimum; _setLevelSwitch = setLevelSwitch; _addOverride = addOverride; }
internal LoggerEnrichmentConfiguration( LoggerConfiguration loggerConfiguration, Action<ILogEventEnricher> addEnricher, Func<ILogEventPropertyFactory> createPropertyValueConverter) { if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration"); if (addEnricher == null) throw new ArgumentNullException("addEnricher"); if (createPropertyValueConverter == null) throw new ArgumentNullException("createPropertyValueConverter"); _loggerConfiguration = loggerConfiguration; _addEnricher = addEnricher; _createPropertyValueConverter = createPropertyValueConverter; }
public void SourceFiltersWorkOnNamespaces() { var written = false; var log = new LoggerConfiguration() .Filter.ByExcluding(Matching.FromSource("Serilog.Tests")) .WriteTo.Sink(new DelegatingSink(e => written = true)) .CreateLogger() .ForContext<MatchingTests>(); log.Write(Some.InformationEvent()); Assert.False(written); }
public void MessageTemplatesCanBeBound() { var log = new LoggerConfiguration() .CreateLogger(); MessageTemplate template; IEnumerable<LogEventProperty> properties; Assert.True(log.BindMessageTemplate("Hello, {Name}!", new object[] { "World" }, out template, out properties)); Assert.Equal("Hello, {Name}!", template.Text); Assert.Equal("World", properties.Single().Value.LiteralValue()); }
public void Configure(LoggerConfiguration configuration) { var directives = CommandLineDslParser.ParseCommandLine(Environment.CommandLine) .ToDictionary(kv => kv.Key, kv => kv.Value); if (!directives.ContainsKey("quiet")) configuration.WriteTo.ColoredConsole(); directives.Add("using:_CommandLineSettingsSerilogFullNetFx", "Serilog.FullNetFx"); configuration.ReadFrom.KeyValuePairs(directives); }
private static void Configure(WebHostBuilderContext ctx, LoggerConfiguration log) { log.WriteTo.Console() .WriteTo.File(@"Logs\.log", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7); }
protected void Application_Start() { //Init logger InitAppInsights(); System.Net.ServicePointManager.DefaultConnectionLimit = 12 * SimpleSettings.NUMBER_OF_PROCESSORS; var config = GlobalConfiguration.Configuration; config.Services.Add(typeof(IExceptionLogger), new TelemetryExceptionLogger()); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; //Analytics logger if (new[] { SimpleSettings.EmailPassword, SimpleSettings.EmailServer, SimpleSettings.EmailUserName, SimpleSettings.FromEmail, SimpleSettings.ToEmails }.All(s => !string.IsNullOrEmpty(s))) { var analyticsLogger = new LoggerConfiguration() .Enrich.With(new UserNameEnricher()) .Destructure.JsonNetTypes() .WriteTo.ApplicationInsightsEvents(AppInsights.TelemetryClient) .CreateLogger(); SimpleTrace.Analytics = analyticsLogger; //Diagnostics Logger var diagnosticsLogger = new LoggerConfiguration() .MinimumLevel.Verbose() .Enrich.With(new UserNameEnricher()) .WriteTo.ApplicationInsightsTraces(AppInsights.TelemetryClient) .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(Matching.WithProperty <int>("Count", p => p % 10 == 0)) .WriteTo.Email(new EmailConnectionInfo { EmailSubject = "TryAppService Alert", EnableSsl = true, FromEmail = SimpleSettings.FromEmail, MailServer = SimpleSettings.EmailServer, NetworkCredentials = new NetworkCredential(SimpleSettings.EmailUserName, SimpleSettings.EmailPassword), Port = 587, ToEmail = SimpleSettings.ToEmails }, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Fatal)) .CreateLogger(); SimpleTrace.Diagnostics = diagnosticsLogger; } else { var logger = new LoggerConfiguration().CreateLogger(); SimpleTrace.Diagnostics = logger; SimpleTrace.Analytics = logger; } SimpleTrace.Diagnostics.Information("Application started"); //Configure Json formatter GlobalConfiguration.Configuration.Formatters.Clear(); GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter()); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Error = (sender, args) => { SimpleTrace.Diagnostics.Error(args.ErrorContext.Error.Message); args.ErrorContext.Handled = true; }; //Templates Routes RouteTable.Routes.MapHttpRoute("templates", "api/templates", new { controller = "Templates", action = "Get", authenticated = false }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("arm-template", "api/armtemplate/{templateName}", new { controller = "Templates", action = "GetARMTemplate", authenticated = false }, new { verb = new HttpMethodConstraint("GET") }); //Telemetry Routes RouteTable.Routes.MapHttpRoute("post-telemetry-event", "api/telemetry/{telemetryEvent}", new { controller = "Telemetry", action = "LogEvent", authenticated = false }, new { verb = new HttpMethodConstraint("POST") }); RouteTable.Routes.MapHttpRoute("post-feedback-comment", "api/feedback", new { controller = "Telemetry", action = "LogFeedback", authenticated = false }, new { verb = new HttpMethodConstraint("POST") }); //Resources Api Routes RouteTable.Routes.MapHttpRoute("get-resource", "api/resource", new { controller = "Resource", action = "GetResource", authenticated = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("get-username", "api/resource/user", new { controller = "Resource", action = "GetUserIdentityName", authenticated = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("create-resource", "api/resource", new { controller = "Resource", action = "CreateResource", authenticated = true }, new { verb = new HttpMethodConstraint("POST") }); RouteTable.Routes.MapHttpRoute("get-webapp-publishing-profile", "api/resource/getpublishingprofile", new { controller = "Resource", action = "GetWebAppPublishingProfile", authenticated = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("get-webapp-content", "api/resource/getwebappcontent/{siteName}", new { controller = "Resource", action = "GetWebAppContent", authenticated = false }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("get-mobile-client-app", "api/resource/mobileclient/{platformString}", new { controller = "Resource", action = "GetMobileClientZip", authenticated = false }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("delete-resource", "api/resource", new { controller = "Resource", action = "DeleteResource", authenticated = true }, new { verb = new HttpMethodConstraint("DELETE") }); RouteTable.Routes.MapHttpRoute("get-resource-status", "api/resource/status", new { controller = "Resource", action = "GetResourceStatus", authenticated = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("extend-resource-expiration-time", "api/resource/extend", new { controller = "Resource", action = "ExtendResourceExpirationTime", authenticated = true }, new { verb = new HttpMethodConstraint("POST") }); //Admin Only Routes RouteTable.Routes.MapHttpRoute("get-all-resources", "api/resource/all/{showFreeResources}", new { controller = "Resource", action = "All", authenticated = true, adminOnly = true, showFreeResources = RouteParameter.Optional }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("reset-all-free-resources", "api/resource/reset", new { controller = "Resource", action = "Reset", authenticated = true, adminOnly = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("reload-all-free-resources", "api/resource/reload", new { controller = "Resource", action = "DropAndReloadFromAzure", authenticated = true, adminOnly = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("delete-users-resource", "api/resource/delete/{userIdentity}", new { controller = "Resource", action = "DeleteUserResource", authenticated = true, adminOnly = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("cleanup-subscriptions", "api/resource/runcleanup", new { controller = "Resource", action = "RunCleanupSubscriptions", authenticated = true, adminOnly = true }, new { verb = new HttpMethodConstraint("GET") }); //Register auth provider SecurityManager.InitAuthProviders(); ResourcesManager.GetInstanceAsync().ConfigureAwait(false).GetAwaiter().GetResult(); }
private static void UpdateLoggerConfiguration(HostBuilderContext hostBuilderContext, LoggerConfiguration currentLoggerConfiguration) { currentLoggerConfiguration .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.FromLogContext() .Enrich.WithVersion() .Enrich.WithComponentName("Promitor Generator") .WriteTo.Console() .WriteTo.AzureApplicationInsights("<application-insights-key>"); }
/// <summary> /// 创建日志 /// </summary> public static Logger CreateLog <T>(IConfiguration configuration = null, string appName = null, string apiKey = null, string serverUrl = null, string outputTemplate = null) { appName ??= configuration?["appName"] ?? "none"; apiKey ??= configuration?["exceptionless:key"]; serverUrl ??= configuration?["exceptionless:url"]; outputTemplate ??= configuration?["outputTemplate"] ?? "{Timestamp:HH:mm:ss} {Level:u3} {userName} 【{Message:lj}】 {SourceContext:l}{NewLine}{Exception}"; var directory = typeof(T).Assembly.GetName().Name; var path = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { path = $"c:\\logs\\{directory}\\"; } else { path = "logs"; } var conf = new LoggerConfiguration() #if DEBUG .MinimumLevel.Verbose() #else .MinimumLevel.Information() #endif .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("System", LogEventLevel.Warning) .Enrich.WithProperty("appName", appName) .WriteTo.Console() .WriteTo.Debug() .WriteTo.Logger(fileLogger => { fileLogger .Filter.ByIncludingOnly(xx => { return(xx.Level > LogEventLevel.Warning); }) .WriteTo.Async(x => { x.File(Path.Combine(path, $"log-{Process.GetCurrentProcess().Id}-.err"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: null, outputTemplate: outputTemplate); }); }) .WriteTo.Logger(fileLogger => { fileLogger .Filter.ByIncludingOnly(xx => { return(xx.Level == LogEventLevel.Warning); }) .WriteTo.Async(x => { x.File(Path.Combine(path, $"log-{Process.GetCurrentProcess().Id}-.wrn"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: null, outputTemplate: outputTemplate); }); }) .WriteTo.Logger(fileLogger => { fileLogger .MinimumLevel.Verbose() .Filter.ByIncludingOnly(xx => xx.Level < LogEventLevel.Warning) .WriteTo.Async(x => { x.File(Path.Combine(path, $"log-{Process.GetCurrentProcess().Id}-.inf"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: null, outputTemplate: outputTemplate); }); if (configuration != null) { fileLogger.ReadFrom.Configuration(configuration); } }); if (!string.IsNullOrEmpty(apiKey) && !string.IsNullOrEmpty(serverUrl)) { Exceptionless.ExceptionlessClient.Default.Startup(apiKey); Exceptionless.ExceptionlessClient.Default.Configuration.ServerUrl = serverUrl; conf.WriteTo.Exceptionless(); } if (configuration != null) { conf.ReadFrom.Configuration(configuration); } return(conf.CreateLogger()); }
internal static async Task Main(string[] args) { Console.WriteLine(@" __ __ __ /'\_/`\ /\ \/\ \ /\ \ /\ \ __ ____ ____\ \ \_\ \ __ __\ \ \____ \ \ \__\ \ /'__`\ /',__\ /',__\\ \ _ \/\ \/\ \\ \ '__`\ \ \ \_/\ \/\ \L\.\_/\__, `\/\__, `\\ \ \ \ \ \ \_\ \\ \ \L\ \ \ \_\\ \_\ \__/.\_\/\____/\/\____/ \ \_\ \_\ \____/ \ \_,__/ \/_/ \/_/\/__/\/_/\/___/ \/___/ \/_/\/_/\/___/ \/___/ "); var version = Assembly.GetEntryAssembly()?.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion; Console.WriteLine($"GitHub modification en masse - {version ?? "1.0"}"); var levelSwitch = new LoggingLevelSwitch(); var loggerConfiguration = new LoggerConfiguration() .MinimumLevel.ControlledBy(levelSwitch) .WriteTo.Console(); var rootCommand = new RootCommand { new Option <string?>("--token", () => null, "Set output to be verbose"), new Option <string?>(new[] { "--org", "-o" }, () => null, "Set organisation to use for all requests"), new Option <string>(new[] { "--product-header" }, () => "mass-hub", "Optionally set a custom product header used when interacting with GitHub API"), new Option <bool>("--verbose", () => false, "Set output to be verbose"), new Option <string?>("--log-file", () => null, "Path to log file"), }; rootCommand.Description = "MassHub - GitHub Management en masse"; rootCommand.Handler = CommandHandler.Create <string?, string?, string, bool, string?>(async(token, org, productHeader, verbose, logFile) => { if (verbose) { levelSwitch.MinimumLevel = LogEventLevel.Debug; } if (!string.IsNullOrWhiteSpace(logFile)) { loggerConfiguration .WriteTo.File(logFile !); } Log.Logger = loggerConfiguration.CreateLogger(); while (string.IsNullOrWhiteSpace(token)) { Log.Debug("GitHub token not provided during argument compile"); Console.WriteLine("GitHub token not provided, please provide a token now to use for authentication, find a token at: https://github.com/settings/tokens"); token = Console.ReadLine(); } while (string.IsNullOrWhiteSpace(org)) { Log.Debug("GitHub organisation not provided during argument compile"); Console.WriteLine("GitHub organisation not provided, please provide the name of the organisation to use for all requests"); org = Console.ReadLine(); } Log.Debug("Starting GitHub client with provided options"); Log.Information("Contacting GitHub with provided token under product header {Header}", productHeader); await RunGitHubService(token !, productHeader, org !); }); await rootCommand.InvokeAsync(args); }
/// <summary> /// A tool to assist with exporting a Player.IO game. /// </summary> /// <param name="username"> The username of your Player.IO account </param> /// <param name="password"> The password of your Player.IO account </param> /// <param name="gameId"> The ID of the game to export. For example: tictactoe-vk6aoralf0yflzepwnhdvw </param> /// <param name="importFolder"> A directory containing the .ZIP BigDB export files given to you by Player.IO. </param> static async Task Main(string username, string password, string gameId, string importFolder) { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(gameId) || string.IsNullOrEmpty(importFolder)) { Console.WriteLine("Unable to launch program. An argument may be missing or invalid. To view the required arguments, use -h."); return; } #region header Console.WriteLine(@" ╔═╗┬ ┌─┐┬ ┬┌─┐┬─┐ ╦╔═╗ ╠═╝│ ├─┤└┬┘├┤ ├┬┘ ║║ ║ ╩ ┴─┘┴ ┴ ┴ └─┘┴└─o╩╚═╝ ╔═╗─┐ ┬┌─┐┌─┐┬─┐┌┬┐ ╔╦╗┌─┐┌─┐┬ ║╣ ┌┴┬┘├─┘│ │├┬┘ │ ║ │ ││ ││ ╚═╝┴ └─┴ └─┘┴└─ ┴ ╩ └─┘└─┘┴─┘ ================================= by https://github.com/atillabyte/"); #endregion var log = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); DeveloperAccount developer; DeveloperGame game; Client client; if (!Directory.Exists(importFolder)) { log.Error("Unable to export game. The input directory specified does not exist."); return; } var archive_files = Directory.GetFiles(importFolder, "*.zip", SearchOption.TopDirectoryOnly).ToList(); if (archive_files.Count == 0) { log.Error("Unable to export game. The input directory specified does not contain any .ZIP export files."); return; } // attempt to login and select game try { developer = await AutoPIO.LoginAsync(username, password); log.Information("Signed in as: " + developer.Username + " (" + developer.Email + ")"); } catch { log.Error("Unable to export game. The login details provided were invalid."); return; } try { game = developer.Games.FirstOrDefault(game => game.GameId == gameId); log.Information("Selected game: " + game.Name + " (" + game.GameId + ")"); } catch { log.Error("Unable to export game. No game was found matching the specified gameId."); return; } // delete export connection if already exists while (true) { var connections = await game.LoadConnectionsAsync(); if (!connections.Any(c => c.Name == "export")) { break; } log.Information("An existing export connection was found - attempting to recreate it. This process should only take a few seconds."); await game.DeleteConnectionAsync(connections.First(c => c.Name == "export")); // wait a second (we don't want to spam) await Task.Delay(1000); } var shared_secret = CreateSharedSecret(username, password, game.GameId); var tables = (await game.LoadBigDBAsync()).Tables; log.Information("Now attempting to create export connection with shared_secret = " + shared_secret); await game.CreateConnectionAsync("export", "A connection with read access to all BigDB tables - used for exporting games.", DeveloperGame.AuthenticationMethod.BasicRequiresAuthentication, "Default", tables.Select(t => (t, true, false, false, false, false, false)).ToList(), shared_secret); // ensure the export connection exists before continuing while (true) { var connections = await game.LoadConnectionsAsync(); if (connections.Any(c => c.Name == "export")) { break; } log.Information("Waiting until we have confirmation that the export connection exists..."); Thread.Sleep(1000); // we don't want to spam. } log.Information("The export connection has been created."); // connect to the game and start export process. try { client = VenturePIO.Connect(game.GameId, "export", "user", VenturePIO.CalcAuth256("user", shared_secret)); } catch (Exception ex) { log.Error("Unable to export game. An error occurred while trying to authenticate with the export connection. Details: " + ex.Message); return; } log.Information("Connected to the game. The export process will now begin."); var export_tasks = new List <Task <List <DatabaseObject> > >(); var progress_bars = new ConcurrentBag <ProgressBar>(); foreach (var archive_file in archive_files) { var split = new FileInfo(archive_file).Name.Split('_'); var game_name = split[0]; var table = split[1]; var game_db = split[2]; // create output directory var output_directory = Path.Combine("exports", game_name, table, game_db); // ensure output directory exists. Directory.CreateDirectory(output_directory); // find all keys in table export as fujson. var archive_keys = GetDatabaseObjectKeysFromArchive(archive_file); var already_exported = Directory.GetDirectories(output_directory, "*", SearchOption.TopDirectoryOnly).Select(x => new DirectoryInfo(x).Name).ToList(); // add progress bar to the console var progress_bar = new ProgressBar(PbStyle.DoubleLine, archive_keys.Count); progress_bars.Add(progress_bar); progress_bar.Refresh(0, table); export_tasks.Add(ProcessJob(client, output_directory, table, archive_keys, progress_bar)); } Task.WaitAll(export_tasks.ToArray()); Console.WriteLine(); log.Information("The export process has completed successfully. You can now close the program."); Console.ReadLine(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApiVersionDescriptionProvider provider) { app.UseHealthCheck("/HealthCheck", null, new TimeSpan(0, 0, 10)); loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); //-------------------------------------------------------serilog 配置 MVCLogOptions mvcLogOptions = new MVCLogOptions() { LogPath = "D:\\LogFiles_AuthAdmin_API",//Configuration[nameof(AuthLogOptions.LogPath)], PathFormat = "{Date}.log" }; var serilog = new LoggerConfiguration() .MinimumLevel.Debug() .Enrich.FromLogContext() .WriteTo.RollingFile(Path.Combine(mvcLogOptions.LogPath, mvcLogOptions.PathFormat), outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {Message}{NewLine}{Exception}"); MVCLogOptions.EnsurePreConditions(mvcLogOptions); loggerFactory.AddSerilog(serilog.CreateLogger()); // Ensure any buffered events are sent at shutdown 日志的生命周期 IApplicationLifetime appLifetime = (IApplicationLifetime)app.ApplicationServices.GetService(typeof(IApplicationLifetime)); if (appLifetime != null) { appLifetime.ApplicationStopped.Register(Serilog.Log.CloseAndFlush); } app.UseAuthLog(mvcLogOptions);//这个中间件用作记录请求中的过程日志 //---------------------------------------------------serilog 配置 //app.UsePetapoco();//use Petapocomiddleware app.UseDapper(); //----dapper if (env.IsDevelopment()) { //开发环境异常处理 app.UseDeveloperExceptionPage(); } else { //生产环境异常处理 app.UseExceptionHandler("/Shared/Error"); } //使用静态文件 app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()) }); ////Session //app.UseSession(new SessionOptions() { IdleTimeout = TimeSpan.FromMinutes(30) }); SeedData.Initialize(app.ApplicationServices); //EF初始化数据 app.UseMvcWithDefaultRoute(); app.UseSwagger() .UseSwaggerUI(options => { // build a swagger endpoint for each discovered API version foreach (var description in provider.ApiVersionDescriptions) { options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant()); } }); //往zookeeper注册服务 AuthAPIRegister.registerAuthAPIHostPort(Configuration.GetSection("UrlConfig").GetValue <string>("ZooKeeperList")); //注册到eureka,springcloud服务发现的注册 //app.UseDiscoveryClient(); }
public static void AddSerilog(this IServiceCollection services) { var config = EngineContext.Current.Resolve <AppConfig>(); var logEventLevel = GetLogEventLevel(config.LogEventLevel); var rollingInterval = GetRollingInterval(config.RollingInterval); var serilog = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.File(config.LogFilePath, logEventLevel, rollingInterval: rollingInterval) .CreateLogger(); //启动时创建logger对象,静态方法,后面所有地方通过类似Log.Debug()方式调用,无需再创建logger对象 Log.Logger = serilog; //注入.net core 日志框架 var loggerFactory = EngineContext.Current.Resolve <ILoggerFactory>(); loggerFactory.AddSerilog(serilog); //系统遇到问题继续把内存中的日志信息输出到日志文件 IApplicationLifetime appLifetime = EngineContext.Current.Resolve <IApplicationLifetime>(); if (appLifetime != null) { appLifetime.ApplicationStopped.Register(Log.CloseAndFlush); } LogEventLevel GetLogEventLevel(string logLevel) { switch (logLevel) { case "Verbose": return(LogEventLevel.Verbose); case "Debug": return(LogEventLevel.Debug); case "Information": return(LogEventLevel.Information); case "Warning": return(LogEventLevel.Warning); case "Error": return(LogEventLevel.Error); case "Fatal": return(LogEventLevel.Fatal); default: return(LogEventLevel.Error); } } RollingInterval GetRollingInterval(string interval) { switch (interval) { case "Infinite": return(RollingInterval.Infinite); case "Year": return(RollingInterval.Year); case "Day": return(RollingInterval.Day); case "Month": return(RollingInterval.Month); case "Hour": return(RollingInterval.Hour); case "Minute": return(RollingInterval.Minute); default: return(RollingInterval.Hour); } } }
public static LoggerConfiguration Build(IConfiguration config) { if (config == null) { throw new System.ArgumentNullException(nameof(config)); } LoggerConfiguration loggerConfig = new LoggerConfiguration() .ReadFrom.Configuration(config) .Enrich.WithProperty(LoggingEnrichment.Application, Assembly.GetExecutingAssembly().GetName().Name) .Enrich.WithProperty(LoggingEnrichment.Version, Version.GetShortVersion()); string instance = config[ConfigurationKey.InstanceName]; if (!string.IsNullOrEmpty(instance)) { loggerConfig.Enrich.WithProperty(LoggingEnrichment.Instance, instance); } loggerConfig.Enrich.FromLogContext() .WriteTo.Console(); string rollingLogLocation = Path.Combine("shared", config[ConfigurationKey.RollingLogPath]); if (!string.IsNullOrEmpty(rollingLogLocation)) { string rollingLogFile = !string.IsNullOrEmpty(instance) ? Path.Combine(rollingLogLocation, $"log-{instance}-{{Date}}.txt") : Path.Combine(rollingLogLocation, "log-{Date}.txt"); loggerConfig.WriteTo.Logger(_ => _ .Filter.ByExcluding(Matching.FromSource(ErrorControllerName)) .WriteTo.RollingFile(rollingLogFile)); string httpErrorFileTag = config[ConfigurationKey.RollingLogHttp]; if (!string.IsNullOrEmpty(httpErrorFileTag)) { string httpLogFile = !string.IsNullOrEmpty(instance) ? Path.Combine(rollingLogLocation, $"{httpErrorFileTag}-{instance}-{{Date}}.txt") : Path.Combine(rollingLogLocation, $"{httpErrorFileTag}-{{Date}}.txt"); loggerConfig.WriteTo.Logger(_ => _ .Filter.ByIncludingOnly(Matching.FromSource(ErrorControllerName)) .WriteTo.RollingFile(httpLogFile)); } } string sqlLog = config.GetConnectionString(ConfigurationKey.CSSqlServerSerilog); if (!string.IsNullOrEmpty(sqlLog)) { loggerConfig .WriteTo.Logger(_ => _ .Filter.ByExcluding(Matching.FromSource(ErrorControllerName)) .WriteTo.MSSqlServer(sqlLog, new MSSqlServerSinkOptions { TableName = "Logs", AutoCreateSqlTable = true }, restrictedToMinimumLevel: LogEventLevel.Information, columnOptions: new ColumnOptions { AdditionalColumns = new[] { new SqlColumn(LoggingEnrichment.Application, SqlDbType.NVarChar, dataLength: 255), new SqlColumn(LoggingEnrichment.Version, SqlDbType.NVarChar, dataLength: 255), new SqlColumn(LoggingEnrichment.Instance, SqlDbType.NVarChar, dataLength: 255), new SqlColumn(LoggingEnrichment.RemoteAddress, SqlDbType.NVarChar, dataLength: 255) } })); } if (!string.IsNullOrEmpty(config[ConfigurationKey.SeqEndpoint])) { loggerConfig.WriteTo.Logger(_ => _ .Filter.ByExcluding(Matching.FromSource(ErrorControllerName)) .WriteTo.Seq(config[ConfigurationKey.SeqEndpoint], apiKey: config[ConfigurationKey.SeqApiKey])); } return(loggerConfig); }
public void ConfigureServices(IServiceCollection services) { if (HostingEnvironment.IsDevelopment()) { var envFilepath = Configuration.GetValue <string>("EnvFilepath") ?? null; if (!string.IsNullOrEmpty(envFilepath) && File.Exists(envFilepath)) { DotNetEnv.Env.Load(envFilepath); } } services.AddSingleton(Configuration); var dbSettings = OptionsClient.GetData(Configuration.GetSection("Database").Get <DatabaseSettings>()); services.AddSingleton(dbSettings); var jwtSettings = OptionsClient.GetData(Configuration.GetSection("Jwt").Get <JwtSettings>()); services.AddSingleton(jwtSettings); var emailSettings = OptionsClient.GetData(Configuration.GetSection("Email").Get <EmailSettings>()); services.AddSingleton(emailSettings); var googleSettings = OptionsClient.GetData(Configuration.GetSection("Google").Get <GoogleSettings>()); services.AddSingleton(googleSettings); var fileSettings = OptionsClient.GetData(Configuration.GetSection("FileStore").Get <FileStoreSettings>()); services.AddSingleton(fileSettings); var logger = new LoggerConfiguration() .ReadFrom.Configuration(Configuration) .WriteTo.LokiHttp( new NoAuthCredentials(OptionsClient.GetData(Configuration.GetValue <string>("Serilog:LokiUrl"))), new LogLabelProvider(Configuration, HostingEnvironment) ) .Destructure.ByMaskingProperties("Password", "Token") .CreateLogger(); services.AddSingleton <ILogger>(logger); services .AddMvcCore(options => { options.Filters.Add(typeof(ModelValidationFilter)); }) .AddCors() .AddControllersAsServices() .AddFormatterMappings() .AddNewtonsoftJson() .AddApiExplorer() .AddDataAnnotations(); services .AddResponseCompression() .AddControllers(); services .AddLocalization() .AddMemoryCache(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddHttpClient(HttpClientNames.GoogleRecaptcha); services.AddEntityFrameworkNpgsql().AddDbContextPool <TextBookerContext>(options => { options.UseNpgsql(dbSettings.ConnectionString, builder => { builder.EnableRetryOnFailure(); }); options.UseSnakeCaseNamingConvention(); options.EnableSensitiveDataLogging(false); options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }, dbSettings.PoolSize); services .AddHealthChecks() .AddDbContextCheck <TextBookerContext>(); services .AddAuthorization() .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = jwtSettings.Issuer, ValidAudience = jwtSettings.Issuer, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Key)), ClockSkew = TimeSpan.FromDays(30) }; }); var swaggerTitle = Configuration.GetValue <string>("SystemInfo:Name"); services.AddSwaggerGen(c => { c.SwaggerDoc("v1.0", new OpenApiInfo { Title = swaggerTitle, Version = "v1.0" }); var currentAssembly = Assembly.GetExecutingAssembly(); var xmlDocs = currentAssembly .GetReferencedAssemblies() .Union(new AssemblyName[] { currentAssembly.GetName() }) .Select(a => Path.Combine(Path.GetDirectoryName(currentAssembly.Location), $"{a.Name}.xml")) .Where(f => File.Exists(f)).ToArray(); Array.ForEach(xmlDocs, (d) => { c.IncludeXmlComments(d); }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new OpenApiSecurityRequirement() { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header, }, Array.Empty <string>() } }); }); var mapper = new MapperConfiguration(cfg => cfg.AddMaps(BusinessLogicAssembly.Value)).CreateMapper(); services.AddSingleton(mapper); services.AddMetrics(Program.Metrics); services.AddSingleton <IVersionService, VersionService>(); services.AddTransient <IMailSender, MailSender>(); services.AddTransient <ICommonService, CommonService>(); services.AddTransient <IUserService, UserService>(); services.AddTransient <ISiteSettingsService, SiteSettingsService>(); services.AddTransient <IPageService, PageService>(); services.AddTransient <IBlockService, BlockService>(); services.AddTransient <IFileService, FileService>(); services.AddTransient <ISiteGenerator, SiteGenerator>(); }
private void SetupConfiguration(LoggerConfiguration configuration) { configuration.Enrich.With(new SourceEnricher()); // used for enriching logs with class names. configuration.Enrich.With(new ComponentEnricher()); // used for enriching logs with pool names. configuration.MinimumLevel.Verbose(); // lower the default minimum level to verbose as sinks can only rise them but not lower. }
static DatadogLogging() { // No-op for if we fail to construct the file logger SharedLogger = new LoggerConfiguration() .WriteTo.Sink <NullSink>() .CreateLogger(); try { if (GlobalSettings.Source.DebugEnabled) { LoggingLevelSwitch.MinimumLevel = LogEventLevel.Verbose; } var maxLogSizeVar = Environment.GetEnvironmentVariable(ConfigurationKeys.MaxLogFileSize); if (long.TryParse(maxLogSizeVar, out var maxLogSize)) { // No verbose or debug logs MaxLogFileSize = maxLogSize; } var logDirectory = GetLogDirectory(); // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (logDirectory == null) { return; } var currentProcess = Process.GetCurrentProcess(); // Ends in a dash because of the date postfix var managedLogPath = Path.Combine(logDirectory, $"dotnet-tracer-{currentProcess.ProcessName}-.log"); var loggerConfiguration = new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.ControlledBy(LoggingLevelSwitch) .WriteTo.File( managedLogPath, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}{Properties}{NewLine}", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: MaxLogFileSize); try { var currentAppDomain = AppDomain.CurrentDomain; loggerConfiguration.Enrich.WithProperty("MachineName", currentProcess.MachineName); loggerConfiguration.Enrich.WithProperty("ProcessName", currentProcess.ProcessName); loggerConfiguration.Enrich.WithProperty("PID", currentProcess.Id); loggerConfiguration.Enrich.WithProperty("AppDomainName", currentAppDomain.FriendlyName); } catch { // At all costs, make sure the logger works when possible. } SharedLogger = loggerConfiguration.CreateLogger(); } catch { // Don't let this exception bubble up as this logger is for debugging and is non-critical } finally { // Log some information to correspond with the app domain SharedLogger.Information(FrameworkDescription.Create().ToString()); } }
public static IWebHostBuilder CreateWebHostBuilder(string[] args) { string environment = Environment.GetEnvironmentVariable("AppMode"); if (String.IsNullOrWhiteSpace(environment)) { environment = "Production"; } string currentDirectory = Directory.GetCurrentDirectory(); var config = new ConfigurationBuilder() .SetBasePath(currentDirectory) .AddYamlFile("appsettings.yml", optional: true, reloadOnChange: true) .AddYamlFile($"appsettings.{environment}.yml", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .AddCommandLine(args) .Build(); var settings = Settings.ReadFromConfiguration(config, environment); var loggerConfig = new LoggerConfiguration().ReadFrom.Configuration(config); if (!String.IsNullOrEmpty(settings.ExceptionlessApiKey)) { loggerConfig.WriteTo.Sink(new ExceptionlessSink(), LogEventLevel.Verbose); } Log.Logger = loggerConfig.CreateLogger(); Log.Information("Bootstrapping {AppMode} mode API ({InformationalVersion}) on {MachineName} using {@Settings} loaded from {Folder}", environment, Settings.Current.InformationalVersion, Environment.MachineName, Settings.Current, currentDirectory); bool useApplicationInsights = !String.IsNullOrEmpty(Settings.Current.ApplicationInsightsKey); var builder = WebHost.CreateDefaultBuilder(args) .UseEnvironment(environment) .UseKestrel(c => { c.AddServerHeader = false; if (Settings.Current.MaximumEventPostSize > 0) { c.Limits.MaxRequestBodySize = Settings.Current.MaximumEventPostSize; } }) .UseSerilog(Log.Logger) .SuppressStatusMessages(true) .UseConfiguration(config) .ConfigureServices(s => { if (useApplicationInsights) { s.AddSingleton <ITelemetryInitializer, ExceptionlessTelemetryInitializer>(); s.AddHttpContextAccessor(); s.AddApplicationInsightsTelemetry(); } s.AddSingleton(settings); }) .UseStartup <Startup>(); if (useApplicationInsights) { builder.UseApplicationInsights(Settings.Current.ApplicationInsightsKey); } if (settings.EnableMetricsReporting) { settings.MetricsConnectionString = MetricsConnectionString.Parse(settings.MetricsConnectionString?.ConnectionString); ConfigureMetricsReporting(builder); } return(builder); }
public static async Task <int> StartAsync( string[]?args, IReadOnlyDictionary <string, string?> environmentVariables, CancellationTokenSource?cancellationTokenSource = null, IReadOnlyCollection <Assembly>?scanAssemblies = null, object[]?instances = null) { try { args ??= Array.Empty <string>(); if (args.Length > 0) { TempLogger.WriteLine("Started with arguments:"); foreach (string arg in args) { TempLogger.WriteLine(arg); } } bool ownsCancellationToken = cancellationTokenSource is null; if (int.TryParse( environmentVariables.GetValueOrDefault(ConfigurationConstants.RestartTimeInSeconds), out int intervalInSeconds) && intervalInSeconds > 0) { cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(intervalInSeconds)); } var types = new[] { typeof(IKeyValueConfiguration) }; foreach (var type in types) { TempLogger.WriteLine($"Loaded type {type.FullName}"); } scanAssemblies ??= ApplicationAssemblies.FilteredAssemblies(new[] { "Arbor", "Milou" }); foreach (var scanAssembly in scanAssemblies) { foreach (var referencedAssembly in scanAssembly.GetReferencedAssemblies()) { try { AppDomain.CurrentDomain.Load(referencedAssembly); } catch (Exception ex) { TempLogger.WriteLine(ex.ToString()); } } } cancellationTokenSource ??= new CancellationTokenSource(); cancellationTokenSource.Token.Register( () => TempLogger.WriteLine("App cancellation token triggered")); using App <ApplicationPipeline> app = await App <ApplicationPipeline> .CreateAsync( cancellationTokenSource, args, environmentVariables, scanAssemblies, instances ?? Array.Empty <object>()); bool runAsService = app.Configuration.ValueOrDefault(ApplicationConstants.RunAsService) && !Debugger.IsAttached; app.Logger.Information("Starting application {Application}", app.AppInstance); if (intervalInSeconds > 0) { app.Logger.Debug( "Restart time is set to {RestartIntervalInSeconds} seconds for {App}", intervalInSeconds, app.AppInstance); } else if (app.Logger.IsEnabled(LogEventLevel.Verbose)) { app.Logger.Verbose("Restart time is disabled"); } string[] runArgs; if (!args.Contains(ApplicationConstants.RunAsService) && runAsService) { runArgs = args .Concat(new[] { ApplicationConstants.RunAsService }) .ToArray(); } else { runArgs = args; } await app.RunAsync(runArgs); if (!runAsService) { app.Logger.Debug("Started {App}, waiting for web host shutdown", app.AppInstance); await app.Host.WaitForShutdownAsync(cancellationTokenSource.Token); } app.Logger.Information( "Stopping application {Application}", app.AppInstance); if (ownsCancellationToken) { cancellationTokenSource.SafeDispose(); } if (int.TryParse( environmentVariables.GetValueOrDefault(ConfigurationConstants.ShutdownTimeInSeconds), out int shutDownTimeInSeconds) && shutDownTimeInSeconds > 0) { await Task.Delay(TimeSpan.FromSeconds(shutDownTimeInSeconds), CancellationToken.None); } } catch (Exception ex) { await Task.Delay(TimeSpan.FromMilliseconds(2000)); string?exceptionLogDirectory = args?.ParseParameter("exceptionDir"); string logDirectory = (exceptionLogDirectory ?? AppContext.BaseDirectory); string fatalLogFile = Path.Combine(logDirectory, "Fatal.log"); LoggerConfiguration loggerConfiguration = new LoggerConfiguration() .WriteTo.File(fatalLogFile, flushToDiskInterval: TimeSpan.FromMilliseconds(50)); if (environmentVariables.TryGetValue(LoggingConstants.SeqStartupUrl, out string?url) && Uri.TryCreate(url, UriKind.Absolute, out var uri)) { loggerConfiguration = loggerConfiguration.WriteTo.Seq(uri.AbsoluteUri); } Logger logger = loggerConfiguration .MinimumLevel.Verbose() .CreateLogger(); using (logger) { logger.Fatal(ex, "Could not start application"); TempLogger.FlushWith(logger); await Task.Delay(TimeSpan.FromMilliseconds(1000)); } string exceptionLogFile = Path.Combine(logDirectory, "Exception.log"); await File.WriteAllTextAsync(exceptionLogFile, ex.ToString(), Encoding.UTF8); await Task.Delay(TimeSpan.FromMilliseconds(3000)); return(1); } return(0); }
protected override void ConfigureLogging(LoggerConfiguration loggerConfiguration, LogLevelSwitcher logLevelSwitcher) { base.ConfigureLogging(loggerConfiguration, logLevelSwitcher); logLevelSwitcher.Switch.MinimumLevel = LogEventLevel.Debug; }
static void Main(string[] args) { //we are using serilog for sample purposes var log = new LoggerConfiguration() .WriteTo.Console() .MinimumLevel.Debug() .CreateLogger(); Log.Logger = log; log.Information("Startup"); log.Information(SharedConfiguration.AllSettings); //verify that the queue exists var queueName = ConfigurationManager.AppSettings.ReadSetting("QueueName"); var connectionString = ConfigurationManager.AppSettings.ReadSetting("Database"); var queueConnection = new QueueConnection(queueName, connectionString); using (var createQueueContainer = new QueueCreationContainer <SqlServerMessageQueueInit>(serviceRegister => Injectors.AddInjectors(new SerilogAdapter(log), SharedConfiguration.EnableTrace, SharedConfiguration.EnableMetrics, SharedConfiguration.EnableCompression, SharedConfiguration.EnableEncryption, "SqlServerConsumerLinq", serviceRegister), options => Injectors.SetOptions(options, SharedConfiguration.EnableChaos))) { using (var createQueue = createQueueContainer.GetQueueCreation <SqlServerMessageQueueCreation>(queueConnection)) { if (!createQueue.QueueExists) { //the consumer can't do anything if the queue hasn't been created Log.Error( $"Could not find {connectionString}. Verify that you have run the producer, which will create the queue"); return; } } } using (var schedulerContainer = new SchedulerContainer(serviceRegister => Injectors.AddInjectors(new SerilogAdapter(log), SharedConfiguration.EnableTrace, SharedConfiguration.EnableMetrics, SharedConfiguration.EnableCompression, SharedConfiguration.EnableEncryption, "SqlServerConsumerLinq", serviceRegister), options => Injectors.SetOptions(options, SharedConfiguration.EnableChaos))) { using (var scheduler = schedulerContainer.CreateTaskScheduler()) { var factory = schedulerContainer.CreateTaskFactory(scheduler); factory.Scheduler.Configuration.MaximumThreads = 8; //8 background threads factory.Scheduler.Configuration.MaxQueueSize = 1; //allow work to be de-queued but held in memory until a thread is free //note - the same factory can be passed to multiple queue instances - don't dispose the scheduler container until all queues have finished factory.Scheduler.Start(); //the scheduler must be started before passing it to a queue using (var queueContainer = new QueueContainer <SqlServerMessageQueueInit>(serviceRegister => Injectors.AddInjectors(new SerilogAdapter(log), SharedConfiguration.EnableTrace, SharedConfiguration.EnableMetrics, SharedConfiguration.EnableCompression, SharedConfiguration.EnableEncryption, "SqlServerConsumerLinq", serviceRegister))) { using (var queue = queueContainer.CreateConsumerMethodQueueScheduler(queueConnection, factory)) { //set some processing options and start looking for work //in the async model, the worker count is how many threads are querying the queue - the scheduler runs the work queue.Configuration.Worker.WorkerCount = 1; //lets just run 1 thread that queries the database queue.Configuration.HeartBeat.UpdateTime = "sec(*%10)"; //set a heartbeat every 10 seconds queue.Configuration.HeartBeat.MonitorTime = TimeSpan.FromSeconds(15); //check for dead records every 15 seconds queue.Configuration.HeartBeat.Time = TimeSpan.FromSeconds( 35); //records with no heartbeat after 35 seconds are considered dead //an invalid data exception will be re-tried 3 times, with delays of 3, 6 and then finally 9 seconds queue.Configuration.TransportConfiguration.RetryDelayBehavior.Add( typeof(InvalidDataException), new List <TimeSpan> { TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(6), TimeSpan.FromSeconds(9) }); queue.Configuration.MessageExpiration.Enabled = true; queue.Configuration.MessageExpiration.MonitorTime = TimeSpan.FromSeconds(20); //check for expired messages every 20 seconds queue.Start(); Console.WriteLine("Processing messages - press any key to stop"); Console.ReadKey((true)); //if jaeger is using udp, sometimes the messages get lost; there doesn't seem to be a flush() call ? if (SharedConfiguration.EnableTrace) { System.Threading.Thread.Sleep(2000); } } } } } }
/// <summary> /// Add a LogWriter to the Configuration /// </summary> /// <typeparam name="T"></typeparam> /// <param name="setup"></param> /// <param name="writer"></param> /// <returns></returns> public static ServerSetup AddLogWriter <T>(this ServerSetup setup, ILogWriter <T> writer) where T : ILogMessage { LoggerConfiguration.AddLogWriter(writer); return(setup); }
public void ConfigureServices(IServiceCollection services) { var formatElastic = Configuration.GetValue("FormatLogsInElasticFormat", false); // Logger configuration var logConf = new LoggerConfiguration() .ReadFrom.Configuration(Configuration); if (formatElastic) { var logFormatter = new ExceptionAsObjectJsonFormatter(renderMessage: true); logConf.WriteTo.Console(logFormatter); } else { logConf.WriteTo.Console(); } Log.Logger = logConf.CreateLogger(); services.AddMvc().AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); var auth0Section = Configuration.GetSection("Auth0"); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.Authority = auth0Section.GetValue <string>("TenantDomain"); options.Audience = "https://api.gigdata.openplatforms.org"; options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier" }; options.Events = new JwtBearerEvents { OnTokenValidated = async context => { if (string.IsNullOrEmpty(context.Principal.Identity.Name)) { return; } var cache = context.HttpContext.RequestServices.GetRequiredService <IMemoryCache>(); var cacheEntry = cache.Get(context.Principal.Identity.Name); if (cacheEntry != null) { return; } var cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. .SetAbsoluteExpiration(TimeSpan.FromSeconds(60)).SetSize(1); cache.Set(context.Principal.Identity.Name, true, cacheEntryOptions); var userManager = context.HttpContext.RequestServices.GetRequiredService <IUserManager>(); var documentStore = context.HttpContext.RequestServices.GetRequiredService <IDocumentStore>(); using var session = documentStore.OpenAsyncSession(); var auth0Client = context.HttpContext.RequestServices.GetRequiredService <Auth0ManagementApiHttpClient>(); var userInfo = await auth0Client.GetUserProfile(context.Principal.Identity.Name); var user = await userManager.GetOrCreateUserIfNotExists(context.Principal.Identity.Name, session); user.Name = userInfo.Name; var userEmailState = UserEmailState.Unverified; if (userInfo.EmailVerified) { userEmailState = UserEmailState.Verified; } var existingUserEmail = user.UserEmails.SingleOrDefault(ue => string.Equals(ue.Email, userInfo.Email, StringComparison.InvariantCultureIgnoreCase)); if (existingUserEmail == null) //email does not exist at all { user.UserEmails.Add(new UserEmail(userInfo.Email.ToLowerInvariant(), userEmailState)); } else if (existingUserEmail.UserEmailState != UserEmailState.Verified && userEmailState == UserEmailState.Verified) //email has been verified through Auth0 { existingUserEmail.SetEmailState(UserEmailState.Verified); } if (session.Advanced.HasChanges) { await session.SaveChangesAsync(); } } }; }); var rabbitMqConnectionString = Configuration.GetConnectionString("RabbitMq"); services.AddRebus(c => c .Transport(t => t.UseRabbitMqAsOneWayClient(rabbitMqConnectionString)) .Timeouts(t => t.StoreInMemory()) .Routing(r => r.TypeBased() .Map <FetchDataForPlatformConnectionMessage>("platformdatafetcher.input") .Map <PlatformConnectionUpdateNotificationMessage>("platformdatafetcher.input")) ); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
public LaunchManager() { //the following is needed on linux... the current directory must be the Mono executable, which is bad. Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); try { FormIcon = Icon.FromHandle(Resources.IconSmall.GetHicon()); if (File.Exists(_pathLogFile)) { File.Delete(_pathLogFile); } XmlPreferences prefs = new XmlPreferences(); try { prefs = _prefsSerializer.Deserialize(_pathPrefsFile, new XmlPreferences()); } catch (Exception ex) { Command_Display_Error("Read preferences file", _pathPrefsFile, ex, "Special preferences will be reset"); } Preferences = prefs; Logger = new LoggerConfiguration().WriteTo.File(_pathLogFile, LogEventLevel.Verbose).MinimumLevel.Is(Preferences.MinimumEventLevel).CreateLogger(); AppInfoFactory gameInfoFactory; gameInfoFactory = !File.Exists(_pathGameInfoAssembly) ? null : PatchingHelper.LoadAppInfoFactory(_pathGameInfoAssembly); var settings = new XmlSettings(); var history = new XmlHistory(); ; try { history = _historySerializer.Deserialize(_pathHistoryXml, new XmlHistory()); } catch (Exception ex) { Command_Display_Error("Load patching history", _pathHistoryXml, ex, "If the launcher was terminated unexpectedly last time, it may not be able to recover."); } try { settings = _settingsSerializer.Deserialize(_pathSettings, new XmlSettings()); } catch (Exception ex) { Command_Display_Error("Read settings file", _pathSettings, ex, "Patch list and other settings will be reset."); } string folderDialogReason = null; if (settings.BaseFolder == null) { folderDialogReason = "(no game folder has been specified)"; } else if (!Directory.Exists(settings.BaseFolder)) { folderDialogReason = "(the previous game folder does not exist)"; } if (folderDialogReason != null) { if (!Command_SetGameFolder_Dialog(folderDialogReason)) { Command_ExitApplication(); } } else { BaseFolder = settings.BaseFolder; } _home = new guiHome(this) { Icon = FormIcon }; var defaultAppInfo = new AppInfo() { AppName = "No AppInfo.dll", }; AppInfo = gameInfoFactory?.CreateInfo(new DirectoryInfo(BaseFolder)) ?? defaultAppInfo; AppInfo.AppVersion = AppInfo.AppVersion ?? "version??"; var icon = TryOpenIcon(AppInfo.IconLocation) ?? _home.Icon.ToBitmap(); ProgramIcon = icon; Instructions = new DisposingBindingList <PatchInstruction>(); var instructions = new List <XmlInstruction>(); foreach (var xmlPatch in settings.Instructions) { try { Command_Direct_AddPatch(xmlPatch.PatchPath, xmlPatch.IsEnabled); } catch { instructions.Add(xmlPatch); } } var patchList = instructions.Select(x => x.PatchPath).Join(Environment.NewLine); if (patchList.Length > 0) { Command_Display_Error("Load patches on startup.", patchList); } try { PatchingHelper.RestorePatchedFiles(AppInfo, history.Files); } catch (Exception ex) { Command_Display_Error("Restore files", ex: ex); } //File.Delete(_pathHistoryXml); _home.Closed += (sender, args) => Command_ExitApplication(); _icon = new NotifyIcon { Icon = FormIcon, Visible = false, Text = "Patchwork Launcher", ContextMenu = new ContextMenu { MenuItems = { new MenuItem("Quit", (o, e) => Command_ExitApplication()) } } }; File.Delete(_pathHistoryXml); } catch (Exception ex) { Command_Display_Error("Launch the application", ex: ex, message: "The application will now exit."); Command_ExitApplication(); } }
static async Task Main(string[] args) { var log = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); Log.Logger = log; Server server; //Show the user a list of IPs to choose from. List <string> ipAdressesList = new List <string>(); //List of IPs on the machine. string chosenIP = "127.0.0.1"; //Default value is localhost. int chosenPort = 143; //Default value ipAdressesList.Add(chosenIP); //First default IP is localhost ipAdressesList.Add("0.0.0.0"); //Second default IP is 0.0.0.0 //Letting the user choose the server's IP. Log.Logger.Information("Displaying IP addresses..." + Environment.NewLine); Log.Logger.Information("The available IP addresses for this pc:" + Environment.NewLine); Log.Logger.Information("0) " + chosenIP + " (localhost)"); Log.Logger.Information("1) " + ipAdressesList.Last()); //Displaying the availble IP addresses that are not default values.. int i = 2; foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName())) { if (ip.AddressFamily.ToString() == ProtocolFamily.InterNetwork.ToString()) { ipAdressesList.Add(ip.ToString()); Log.Logger.Information(i.ToString() + ") " + ip.ToString()); i++; } } //Choosing the right address. while (true) { //User picks the desired IP address. try { Console.Write(">"); chosenIP = ipAdressesList[Convert.ToInt32(Console.ReadLine())]; Log.Logger.Information("IP Address chosen: " + chosenIP); Console.WriteLine(); break; } catch { Log.Logger.Error("Invalid address."); } } //Creating the TcpListener while (true) { try { Log.Logger.Information("Please choose a port for the server to run on."); Console.Write(">"); chosenPort = Convert.ToInt32(Console.ReadLine()); server = new Server(chosenIP, chosenPort); break; } catch (Exception ex) { Log.Logger.Error(ex.Message); Log.Logger.Error("Port is either taken, or not a valid port at all."); } } try { await server.StartListening(); } catch (Exception ex) { Log.Logger.Error("Error: " + ex.Message); } }
protected override async void RegisterTypes(IContainerRegistry containerRegistry) { Serilog.Core.Logger logger = new LoggerConfiguration() .ReadFrom.AppSettings() .CreateLogger(); containerRegistry.RegisterInstance <ILogger>(logger); containerRegistry.RegisterSingleton <ILoggerFacade, LoggerFacade>(); containerRegistry.RegisterSingleton <NavigationManager>(); containerRegistry.RegisterSingleton <ModulesNavigationView>(); containerRegistry.RegisterSingleton <ModulesNavigationViewModel>(); containerRegistry.RegisterSingleton <ModuleNavigator>(); containerRegistry.Register <IViewContext, ViewContext>(); containerRegistry.RegisterSingleton <IExchangeApiFactory, ExchangeApiFactory>(); containerRegistry.Register <IExchangeService, ExchangeService>(); containerRegistry.Register <ITradeViewConfigurationAccounts, TradeViewConfigurationAccountsFile>(); containerRegistry.Register <ITradeViewConfigurationStrategy, TradeViewConfigurationStrategyFile>(); containerRegistry.Register <ITradeViewConfigurationServer, TradeViewConfigurationServerFile>(); containerRegistry.Register <IAccountsService, AccountsService>(); containerRegistry.Register <IStrategyService, StrategyService>(); containerRegistry.Register <ITradeServerService, TradeServerService>(); containerRegistry.Register <IWpfExchangeService, WpfExchangeService>(); containerRegistry.Register <ISymbolsCache, SymbolsCache>(); containerRegistry.RegisterSingleton <ISymbolsCacheFactory, SymbolsCacheFactory>(); containerRegistry.RegisterSingleton <IServerMonitorCache, ServerMonitorCache>(); containerRegistry.RegisterSingleton <IOrderBookHelperFactory, OrderBookHelperFactory>(); containerRegistry.RegisterSingleton <ITradeHelperFactory, TradeHelperFactory>(); containerRegistry.RegisterSingleton <IHelperFactoryContainer, HelperFactoryContainer>(); containerRegistry.RegisterSingleton <IChartHelper, ChartHelper>(); containerRegistry.Register <OrdersViewModel>(); containerRegistry.Register <AccountBalancesViewModel>(); containerRegistry.Register <AccountViewModel>(); containerRegistry.Register <SymbolsViewModel>(); containerRegistry.Register <TradePanelViewModel>(); containerRegistry.Register <IStrategyFileManager, StrategyFileManager>(); containerRegistry.Register <ISymbolsLoader, SymbolsLoader>(); containerRegistry.Register <IStrategyAssemblyManager, StrategyAssemblyManager>(); containerRegistry.Register <Strategies.ViewModel.SymbolsViewModel>(); containerRegistry.Register <Strategies.ViewModel.StrategyParametersViewModel>(); containerRegistry.RegisterSingleton <IHttpClientManager, HttpClientManager>(); var serverMonitorCache = Container.Resolve <IServerMonitorCache>(); serverMonitorCache.StartObservingServers(); var symbolsCacheFactory = Container.Resolve <ISymbolsCacheFactory>(); await symbolsCacheFactory.SubscribeAccountsAssets().ConfigureAwait(false); }
static async Task Main(string[] args) { using var log = new LoggerConfiguration() //.WriteTo.Console() .WriteTo.File("log.txt") .AddMsSqlServerSink("Server=(localDB)\\MSSQLLocalDB;Initial Catalog=DumpLibrary;Integrated Security=True;") .CreateLogger(); try { using var dbContext = new SampleDesignTimeDbContextFactory().CreateDbContext(args); var authors = await dbContext.Authors.ToListAsync(); foreach (var a in authors) { Console.WriteLine("[{0}] {1}", a.Id, a.FullName); } int optionCode = 0; do { Console.WriteLine("0)Exit\n1)List Books\n2)Insert new Book\n3)Update Book Title\n4)Delete Book\nOption:"); var option = Console.ReadKey(); optionCode = int.Parse(option.KeyChar.ToString()); Console.WriteLine(); switch (optionCode) { case 1: var books = await dbContext.Books.Include(b => b.Author).ToArrayAsync(); foreach (var b in books) { Console.WriteLine("[{0}] {1}:{2}", b.Id, b.Author.FullName, b.Title); } break; case 2: Console.WriteLine("Input Book Id"); var bookId = int.Parse(Console.ReadLine()); Console.WriteLine("Input Book Title"); var bookTitle = Console.ReadLine(); Console.WriteLine("Input Author Id"); var authorId = int.Parse(Console.ReadLine()); var book = new Book() { Id = bookId, AuthorId = authorId, Title = bookTitle }; dbContext.Books.Add(book); await dbContext.SaveChangesAsync(); break; case 3: Console.WriteLine("Input Book Id"); var updateBookId = int.Parse(Console.ReadLine()); Console.WriteLine("Input new Book Title"); var updateBookTitle = Console.ReadLine(); var bookForUpdate = await dbContext.Books.SingleAsync(x => x.Id == updateBookId); bookForUpdate.Title = updateBookTitle; await dbContext.SaveChangesAsync(); break; case 4: Console.WriteLine("Input Book Id"); var deleteBookId = int.Parse(Console.ReadLine()); var bookForDelete = await dbContext.Books.SingleAsync(x => x.Id == deleteBookId); dbContext.Remove(bookForDelete); await dbContext.SaveChangesAsync(); break; } }while (optionCode > 0); } catch (Exception ex) { log.Error(ex, "App error!"); } }
public object Execute(RunArgs args) { Console.WriteLine("Resgrid Email Processor"); Console.WriteLine("-----------------------------------------"); Console.WriteLine("Starting Up"); var _running = true; var model = new RunViewModel(); var config = _configService.LoadSettingsFromFile(); _fileService.SetupDirectories(); TelemetryConfiguration configuration = null; TelemetryClient telemetryClient = null; if (!String.IsNullOrWhiteSpace(config.DebugKey)) { try { configuration = TelemetryConfiguration.Active; configuration.InstrumentationKey = config.DebugKey; configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); configuration.TelemetryInitializers.Add(new HttpDependenciesParsingTelemetryInitializer()); telemetryClient = new TelemetryClient(); System.Console.WriteLine("Application Insights Debug Key Detected and AppInsights Initialized"); } catch { } } Logger log; if (config.Debug) { log = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .CreateLogger(); } else { log = new LoggerConfiguration() .MinimumLevel.Error() .WriteTo.Console() .CreateLogger(); } using (InitializeDependencyTracking(configuration)) { // Define the cancellation token. CancellationTokenSource source = new CancellationTokenSource(); CancellationToken token = source.Token; Thread dataThread = new Thread(() => _dataService.Run(token, log, config)); dataThread.Name = $"Data Service Thread"; dataThread.Start(); Thread emailThread = new Thread(() => _emailService.Run(token, log)); emailThread.Name = $"Email Service Thread"; emailThread.Start(); Thread importThread = new Thread(() => _montiorService.Run(token, log)); importThread.Name = $"Import Service Thread"; importThread.Start(); System.Console.WriteLine("Email Processor is Running..."); System.Console.WriteLine("Press any key to stop"); var line = Console.ReadLine(); source.Cancel(); System.Console.WriteLine("Shutting down, please wait..."); Task.Delay(2000).Wait(); _running = false; } if (telemetryClient != null) { telemetryClient.Flush(); Task.Delay(5000).Wait(); } return(View("Run", model)); }
public static void Initialize(ILogger bootstrappingLogger, string prefix, Action <string, IServiceCollection, ITestRunReporterContext> beforeContainerBuild) { if (_instance != null) { throw new InvalidOperationException($"The Initialize method has already been called. "); } if (prefix == null) { throw new ArgumentNullException(nameof(prefix)); } if (beforeContainerBuild == null) { throw new ArgumentNullException(nameof(beforeContainerBuild)); } Environment.SetEnvironmentVariable("TEST_OUTPUT_FOLDER", Directory.GetCurrentDirectory(), EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("TEST_DEPLOYMENT_FOLDER", Directory.GetCurrentDirectory(), EnvironmentVariableTarget.Process); // // TODO: When this gets too big, look at Factories // var services = new ServiceCollection(); RegisterDeviceSettings(bootstrappingLogger, prefix, services); RegisterBrowserSettings(bootstrappingLogger, prefix, services); var instrumentationSettings = ConfigureSettings <IInstrumentationSettings, InstrumentationSettings>(bootstrappingLogger, prefix, "InstrumentationSettings", "common.json", "instrumentationSettings", services); RegisterSettings <RemoteWebDriverSettings>(bootstrappingLogger, prefix, "RemoteWebDriverSettings", "common-localhost-selenium.json", "remoteWebDriverSettings", services, registerInstance: true); RegisterSettings <EnvironmentSettings>(bootstrappingLogger, prefix, "EnvironmentSettings", "internet.json", "environmentSettings", services, registerInstance: true); ConfigureSettings <IControlSettings, ControlSettings>(bootstrappingLogger, prefix, "ControlSettings", "common.json", "controlSettings", services); // Clear the variables so they do not creep into the rest of our implementation Environment.SetEnvironmentVariable("TEST_OUTPUT_FOLDER", null, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("TEST_DEPLOYMENT_FOLDER", null, EnvironmentVariableTarget.Process); // Singletons: statics that are instantiated once for the lifetime of the entire test run services.AddSingleton <IDriverSessionFactory, DriverSessionFactory>(); var testRunReporterContext = new TestRunReporterContext() { InstrumentationSettings = instrumentationSettings, RootReportingFolder = instrumentationSettings.RootReportingFolder, TestRunIdentity = DateTime.Now.ToString("yyyyMMdd-HHmmss") }; services.AddSingleton <ITestRunReporterContext>(testRunReporterContext); // Scoped: per test services.AddScoped(isp => { var serilogContext = BuildSerilogConfiguration(); var logPath = isp.GetRequiredService <ITestCaseReporterContext>().LogFilePath; LoggerConfiguration loggerConfiguration = new LoggerConfiguration() .ReadFrom .Configuration(serilogContext) .Enrich .FromLogContext(); if (isp.GetRequiredService <IInstrumentationSettings>().LogFilePerTest) { loggerConfiguration.WriteTo .File(logPath); } ; ILogger logger = loggerConfiguration.CreateLogger(); return(logger); }); services.AddScoped <ICommandExecutor>(isp => { var remoteWebDriverSettings = isp.GetRequiredService <RemoteWebDriverSettings>(); var commandExecutor = new HttpCommandExecutor(new Uri(remoteWebDriverSettings.RemoteUri), TimeSpan.FromSeconds(remoteWebDriverSettings.HttpCommandExecutorTimeoutInSeconds)); return(commandExecutor); }); services.AddScoped(isp => { var factory = isp.GetRequiredService <IDriverSessionFactory>(); var browserProperties = isp.GetRequiredService <IBrowserProperties>(); var remoteWebDriverSettings = isp.GetRequiredService <RemoteWebDriverSettings>(); var environmentSettings = isp.GetRequiredService <EnvironmentSettings>(); var controlSettings = isp.GetRequiredService <IControlSettings>(); var deviceSettings = isp.GetRequiredService <IDeviceProperties>(); var logger = isp.GetRequiredService <ILogger>(); var testCaseReporter = isp.GetRequiredService <ITestCaseReporter>(); var httpCommandExecutor = isp.GetRequiredService <ICommandExecutor>(); var driverSession = factory.Create(deviceSettings, browserProperties, remoteWebDriverSettings, environmentSettings, controlSettings, logger, testCaseReporter, httpCommandExecutor); return(driverSession); }); beforeContainerBuild(prefix, services, testRunReporterContext); var reportingContextManager = new ReportingContextRegistrationManager(bootstrappingLogger, services, testRunReporterContext); reportingContextManager.AssertIsNotPartiallyConfigured(); if (!reportingContextManager.IsConfigured) { reportingContextManager.PopulateDefaultReportingContexts(); } _instance = services.BuildServiceProvider(); }
static void Main(string[] args) { bool running; bool connected; int maxPacketLen = (int)Math.Pow(2, 13); ILogger logger = new LoggerConfiguration().WriteTo.File("log.txt", rollingInterval: RollingInterval.Month).CreateLogger(); ICommunicationService communicationService = new SerialService(logger); IAudioCaptureService audioCaptureService = new NAudioCaptureService(96000, maxPacketLen, logger); IFFTService fftService = new FFTService(); ILedService ledService = new AdafruitLedService(communicationService, fftService); running = true; // Attempt to connect to the device and start the equalizer. try { AttemptConnect(ledService, logger); ledService.EqualizerStart(maxPacketLen, audioCaptureService); connected = true; } catch (Exception e) { connected = false; logger.Error("Unable to connect to device: " + e.ToString()); } // USEFUL DEBUG: Print everything to the Console. communicationService.ResponseSubscribe((response) => { Console.WriteLine(JsonConvert.SerializeObject(response)); }); // Listen for user input. Console.WriteLine("Yo hit the spacebar to hit the lights bro, P to ping, Q/ESC to quit"); while (running) { try { Console.Write('>'); ConsoleKeyInfo input = Console.ReadKey(); switch (Char.ToUpper(input.KeyChar)) { case ' ': { if (connected == true) { ledService.Disconnect(); connected = false; } else { AttemptConnect(ledService, logger); ledService.EqualizerStart(maxPacketLen, audioCaptureService); connected = true; } } break; case 'P': ledService.Ping(5000); break; case 'Q': case (char)27: // ESC running = false; ledService.Disconnect(); break; default: break; } Console.WriteLine(); } catch (Exception e) { Console.WriteLine(">:( Okay now how did we get here?\n" + e.ToString()); continue; //momma didn't raise no quitter! } } }
private void LoadLogsintoElastic(Guid executionId, Guid resourceId, string auditType, string detail, LogLevel eventLevel) { var dependency = new Depends(Depends.ContainerType.AnonymousElasticsearch); var hostName = "http://" + dependency.Container.IP; var port = dependency.Container.Port; var loggerSource = new SerilogElasticsearchSource { Port = port, HostName = hostName, SearchIndex = "warewolftestlogs" }; var uri = new Uri(hostName + ":" + port); var logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.Sink(new ElasticsearchSink(new ElasticsearchSinkOptions(uri) { AutoRegisterTemplate = true, IndexDecider = (e, o) => loggerSource.SearchIndex, })) .CreateLogger(); var mockSeriLogConfig = new Mock <ISeriLogConfig>(); mockSeriLogConfig.SetupGet(o => o.Logger).Returns(logger); var mockDataObject = new Mock <IDSFDataObject>(); using (var loggerConnection = loggerSource.NewConnection(mockSeriLogConfig.Object)) { var loggerPublisher = loggerConnection.NewPublisher(); if (eventLevel == LogLevel.Error) { mockDataObject = SetupDataObjectWithAssignedInputsAndError(executionId, resourceId); } else { mockDataObject = SetupDataObjectWithAssignedInputs(executionId, resourceId); } var auditLog = new Audit(mockDataObject.Object, auditType, detail, null, null); //-------------------------Act---------------------------------- switch (eventLevel) { case LogLevel.Debug: loggerPublisher.Debug(GlobalConstants.WarewolfLogsTemplate, auditLog); break; case LogLevel.Warn: loggerPublisher.Warn(GlobalConstants.WarewolfLogsTemplate, auditLog); break; case LogLevel.Fatal: loggerPublisher.Fatal(GlobalConstants.WarewolfLogsTemplate, auditLog); break; case LogLevel.Error: loggerPublisher.Error(GlobalConstants.WarewolfLogsTemplate, auditLog); break; default: loggerPublisher.Info(GlobalConstants.WarewolfLogsTemplate, auditLog); break; } } Task.Delay(225).Wait(); }
public static void Execute <TSpider>(params string[] args) { var logfile = Environment.GetEnvironmentVariable("DOTNET_SPIDER_ID"); logfile = string.IsNullOrWhiteSpace(logfile) ? "dotnet-spider.log" : $"/logs/{logfile}.log"; Environment.SetEnvironmentVariable("LOGFILE", logfile); if (Log.Logger == null) { var configure = new LoggerConfiguration() #if DEBUG .MinimumLevel.Verbose() #else .MinimumLevel.Information() #endif .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Console().WriteTo .RollingFile(logfile); Log.Logger = configure.CreateLogger(); } Framework.SetMultiThread(); var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); configurationBuilder.AddCommandLine(args, Framework.SwitchMappings); configurationBuilder.AddEnvironmentVariables(); var configuration = configurationBuilder.Build(); var id = configuration["DOTNET_SPIDER_ID"] ?? Guid.NewGuid().ToString("N"); var name = configuration["DOTNET_SPIDER_NAME"] ?? id; var arguments = Environment.GetCommandLineArgs(); var builder = new SpiderHostBuilder(); builder.ConfigureLogging(b => { #if DEBUG b.SetMinimumLevel(LogLevel.Debug); #else b.SetMinimumLevel(LogLevel.Information); #endif b.AddSerilog(); }); var config = configuration["DOTNET_SPIDER_CONFIG"]; builder.ConfigureAppConfiguration(x => { if (!string.IsNullOrWhiteSpace(config) && File.Exists(config)) { // 添加 JSON 配置文件 x.AddJsonFile(config); } else { if (File.Exists("appsettings.json")) { x.AddJsonFile("appsettings.json"); } } x.AddCommandLine(Environment.GetCommandLineArgs(), Framework.SwitchMappings); x.AddEnvironmentVariables(); }); builder.ConfigureServices(services => { services.AddLocalMessageQueue(); services.AddLocalDownloadCenter(); services.AddDownloaderAgent(x => { x.UseFileLocker(); x.UseDefaultAdslRedialer(); x.UseDefaultInternetDetector(); }); services.AddStatisticsCenter(x => { // 添加内存统计服务 x.UseMemory(); }); }); var spiderType = typeof(TSpider); builder.Register(spiderType); var provider = builder.Build(); var instance = provider.Create(spiderType); if (instance != null) { instance.Name = name; instance.Id = id; instance.RunAsync(arguments).ConfigureAwait(true).GetAwaiter().GetResult(); } else { Log.Logger.Error("Create spider object failed", 0, ConsoleColor.DarkYellow); } }
private static void UseClassifiedAdsLogger(this IWebHostEnvironment env, LoggingOptions options) { var assemblyName = Assembly.GetEntryAssembly()?.GetName().Name; var logsPath = Path.Combine(env.ContentRootPath, "logs"); Directory.CreateDirectory(logsPath); var loggerConfiguration = new LoggerConfiguration(); loggerConfiguration = loggerConfiguration .MinimumLevel.Debug() .Enrich.FromLogContext() .Enrich.With <ActivityEnricher>() .Enrich.WithMachineName() .Enrich.WithEnvironmentUserName() .Enrich.WithProperty("Assembly", assemblyName) .Enrich.WithProperty("Application", env.ApplicationName) .Enrich.WithProperty("EnvironmentName", env.EnvironmentName) .Enrich.WithProperty("ContentRootPath", env.ContentRootPath) .Enrich.WithProperty("WebRootPath", env.WebRootPath) .Enrich.WithExceptionDetails() .Filter.ByIncludingOnly((logEvent) => { if (logEvent.Level >= options.File.MinimumLogEventLevel || logEvent.Level >= options.Elasticsearch.MinimumLogEventLevel) { var sourceContext = logEvent.Properties.ContainsKey("SourceContext") ? logEvent.Properties["SourceContext"].ToString() : null; var logLevel = GetLogLevel(sourceContext, options); return(logEvent.Level >= logLevel); } return(false); }) .WriteTo.File(Path.Combine(logsPath, "log.txt"), fileSizeLimitBytes: 10 * 1024 * 1024, 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}", 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(); }
public static void Main(string[] args) { var localAppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "VisualStudioStarter"); if (!Directory.Exists(localAppDataPath)) { Directory.CreateDirectory(localAppDataPath); } var logFile = Path.Combine(localAppDataPath, "VisualStudioStarter-{Date}.log"); var logger = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.RollingFile(logFile).CreateLogger(); if (args == null || args.Length < 1) { var provider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); var slnFile = provider.GetDirectoryContents(string.Empty).FirstOrDefault(p => StringComparer.InvariantCultureIgnoreCase.Equals(Path.GetExtension(p.Name), ".sln")); if (slnFile == null) { logger.Warning($@"Zu wenige Parameter übergeben, keine Projektmappe in ""{provider.Root}"" gefunden."); return; } args = new[] { $@".\{slnFile.Name}" }; } else if (args.Length != 1) { logger.Warning("Zu viele Parameter übergeben."); return; } var cfg = new ConfigurationBuilder().SetBasePath(localAppDataPath).AddJsonFile("appsettings.json", false, true).Build(); var options = cfg.Get <VisualStudioOptions>(); try { var result = options.GetExecutable(args.First()); logger.Debug($"{args.First()} wird gestartet mit {result}."); var startInfo = new ProcessStartInfo { FileName = result, Arguments = args.First(), RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true }; var processTemp = new Process { StartInfo = startInfo, EnableRaisingEvents = true }; processTemp.Start(); } catch (InvalidOperationException ex) { logger.Warning(ex.Message); } }
static Program() { var log = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.ColoredConsole().CreateLogger(); Serilog.Log.Logger = log; }
public void IfTheLogFolderDoesNotExistItWillBeCreated() { var fileName = Some.String() + "-{Date}.txt"; var temp = Some.TempFolderPath(); var folder = Path.Combine(temp, Guid.NewGuid().ToString()); var pathFormat = Path.Combine(folder, fileName); ILogger log = null; try { log = new LoggerConfiguration() .WriteTo.SizeRollingFile(pathFormat, retainedFileDurationLimit: TimeSpan.FromSeconds(180)) .CreateLogger(); log.Write(Some.InformationEvent()); Assert.True(Directory.Exists(folder)); } finally { var disposable = (IDisposable)log; if (disposable != null) disposable.Dispose(); Directory.Delete(temp, true); } }