Пример #1
0
        public static void Main(string[] args)
        {
            AspNetCoreLogging.Initialize();
            LoggingServices.DefaultBackend = new ConsoleLoggingBackend();

            // This loads the configuration file from disk.
            // You can also store this file in a cloud storage service and use ConfigureFromXmlWithAutoReloadAsync
            // to load it and have it automatically reload every minute or so, so you can dynamically
            // change the verbosity.
            LoggingServices.DefaultBackend.ConfigureFromXml(XDocument.Load("postsharp-logging.config"));

            CreateHostBuilder(args).Build().Run();
        }
Пример #2
0
        public static void Main(string[] args)
        {
            // Configure Serilog to write to the console and to Elastic Search.
            using (var logger = new LoggerConfiguration()
                                .Enrich.WithProperty("Application", typeof(Program).Assembly.GetName().Name)
                                .MinimumLevel.Debug()
                                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
            {
                BatchPostingLimit = 1, // For demo.
                AutoRegisterTemplate = true,
                AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
                EmitEventFailure = EmitEventFailureHandling.ThrowException | EmitEventFailureHandling.WriteToSelfLog,
                FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
            })
                                .WriteTo.Console(
                       outputTemplate:
                       "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Indent:l}{Message:l}{NewLine}{Exception}")
                                .CreateLogger())
            {
                // Configure PostSharp Logging to write to Serilog.
                var backend = new SerilogLoggingBackend(logger);
                backend.Options.IncludeActivityExecutionTime          = true;
                backend.Options.IncludeExceptionDetails               = true;
                backend.Options.SemanticParametersTreatedSemantically = SemanticParameterKind.All;
                backend.Options.IncludedSpecialProperties             = SerilogSpecialProperties.All;
                backend.Options.ContextIdGenerationStrategy           = ContextIdGenerationStrategy.Hierarchical;
                LoggingServices.DefaultBackend = backend;



                // Defines a filter that selects trusted requests.
                // Enabling HTTP Correlation Protocol for communication with untrusted devices is a security risk.
                Predicate <CorrelationRequest> trustedRequests = request => request.RemoteHost == "localhost" ||
                                                                 request.RemoteHost == "127.0.0.1" ||
                                                                 request.RemoteHost == "::1";

                // Instrument ASP.NET Core.
                AspNetCoreLogging.Initialize(correlationProtocol: new LegacyHttpCorrelationProtocol(trustedRequests));


                // Execute the web app.
                CreateWebHostBuilder(args).Build().Run();
            }
        }