Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            // Configure Serilog.
            Serilog.Core.Logger logger = new LoggerConfiguration()
                                         .Enrich.WithProperty("Application", "MicroserviceExample")
                                         .Enrich.FromLogContext()
                                         .MinimumLevel.Debug()
                                         .WriteTo.Async(a => a.DurableHttp(requestUri: "http://localhost:31311", batchFormatter: new ArrayBatchFormatter(), batchPostingLimit: 5))
                                         .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Indent:l}{Message:l}{NewLine}{Exception}")
                                         .CreateLogger();

            // Configure PostSharp.
            SerilogLoggingBackend backend = new SerilogLoggingBackend(logger);

            backend.Options.IncludeExceptionDetails = true;
            backend.Options.SemanticParametersTreatedSemantically |= SemanticParameterKind.MemberName;
            LoggingServices.DefaultBackend = backend;
            LoggingServices.Formatters.Register(typeof(ActionResult <>), typeof(ActionResultFormatter <>));
            LoggingServices.Formatters.Register(new ActionResultFormatter());
            LoggingServices.Formatters.Register(new ObjectResultFormatter());



            // Execute the web app.
            using ( logger )
            {
                CreateWebHostBuilder(args).Build().Run();
            }
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            using (var logger = new LoggerConfiguration()
                                .Enrich.WithProperty("Application", "MicroserviceExample")
                                .MinimumLevel.Debug()
                                .WriteTo.Async(a => a.DurableHttp(requestUri: "http://localhost:31311", batchPostingLimit: 5))
                                .WriteTo.Console(
                       outputTemplate:
                       "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Indent:l}{Message:l}{NewLine}{Exception}")
                                .CreateLogger())
            {
                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;

                LoggingServices.Formatters.Register(typeof(ActionResult <>), typeof(ActionResultFormatter <>));
                LoggingServices.Formatters.Register(new ActionResultFormatter());
                LoggingServices.Formatters.Register(new ObjectResultFormatter());

                // Log only warnings by default, except for 10% randomly chosen requests.
                SampledLoggingActionFilter.Initialize(backend);
                LoggingServices.DefaultBackend.DefaultVerbosity.SetMinimalLevel(LogLevel.Warning);



                // Execute the web app.
                CreateWebHostBuilder(args).Build().Run();
            }
        }
Exemplo n.º 3
0
        private static async Task Main()
        {
            // 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 for which the correlation protocol is enabled.
                // 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";

                // Determines which requests will be logged. We exclude requests to Logstash so that we are not logging
                // the logging itself.
                Predicate <Uri> loggedRequests = uri => uri.Port != 9200;

                // Intercept outgoing HTTP requests and add logging to them.
                HttpClientLogging.Initialize(
                    correlationProtocol: new LegacyHttpCorrelationProtocol(trustedRequests),
                    loggedRequests);


                using (logSource.Debug.OpenActivity(Formatted("Running the client"),
                                                    new OpenActivityOptions(new Baggage {
                    User = "******"
                })))
                {
                    await QueueProcessor.ProcessQueue(".\\My\\Queue");
                }
            }

            Console.WriteLine("Done!");
        }
Exemplo n.º 4
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();
            }
        }
Exemplo n.º 5
0
        private static async Task Main()
        {
            using (var logger = new LoggerConfiguration()
                                .Enrich.WithProperty("Application", "PostSharp.Samples.Logging.ElasticStack.ClientExample")
                                .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())
            {
                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;


                using (logSource.Debug.OpenActivity(Formatted("Running the client"),
                                                    new OpenActivityOptions {
                    Properties = new[] { new LoggingProperty("User", "Gaius Julius Caesar")
                                         {
                                             IsBaggage = true
                                         } }
                }))
                {
                    await QueueProcessor.ProcessQueue(".\\My\\Queue");
                }
            }

            Console.WriteLine("Done!");
        }
Exemplo n.º 6
0
        public static void Main(string[] args)
        {
            using (var logger = new LoggerConfiguration()
                                .Enrich.WithProperty("Application", "PostSharp.Samples.Logging.ElasticStack.MicroserviceExample")
                                .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())
            {
                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;

                //LoggingServices.Formatters.Register(typeof(ActionResult<>), typeof(ActionResultFormatter<>));
                //LoggingServices.Formatters.Register(new ActionResultFormatter());
                //LoggingServices.Formatters.Register(new ObjectResultFormatter());

                // Log only warnings by default, except for 10% randomly chosen requests.
                //SampledLoggingActionFilter.Initialize(backend);
                //LoggingServices.DefaultBackend.DefaultVerbosity.SetMinimalLevel(LogLevel.Warning);



                // Execute the web app.
                CreateWebHostBuilder(args).Build().Run();
            }
        }
Exemplo n.º 7
0
        private static async Task Main()
        {
            using (var logger = new LoggerConfiguration()
                                .Enrich.WithProperty("Application", "ClientExample")
                                .MinimumLevel.Debug()
                                .WriteTo.Async(a => a.DurableHttp(requestUri: "http://localhost:31311", batchPostingLimit: 5))
                                .WriteTo.Console(
                       outputTemplate:
                       "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Indent:l}{Message:l}{NewLine}{Exception}")
                                .CreateLogger())
            {
                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;


                using (logSource.Debug.OpenActivity(Formatted("Running the client"), new OpenActivityOptions
                {
                    Properties = new[]
                    {
                        new LoggingProperty("User", "Gaius Julius Caesar")
                        {
                            IsBaggage = true
                        }
                    }
                }))
                {
                    await QueueProcessor.ProcessQueue(".\\My\\Queue");
                }
            }

            Console.WriteLine("Done!");
        }
Exemplo n.º 8
0
        static async Task Main()
        {
            Serilog.Core.Logger logger = new LoggerConfiguration()
                                         .Enrich.WithProperty("Application", "ClientExample")
                                         .Enrich.FromLogContext()
                                         .MinimumLevel.Debug()
                                         .WriteTo.Async(a => a.DurableHttp(requestUri: "http://localhost:31311", batchFormatter: new ArrayBatchFormatter(), batchPostingLimit: 5))
                                         .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Indent:l}{Message:l}{NewLine}{Exception}")
                                         .CreateLogger();


            SerilogLoggingBackend backend = new SerilogLoggingBackend(logger);

            backend.Options.IncludeExceptionDetails = true;
            backend.Options.SemanticParametersTreatedSemantically |= SemanticParameterKind.MemberName;
            LoggingServices.DefaultBackend = backend;

            using ( logger )
            {
                await QueueProcessor.ProcessQueue(".\\My\\Queue");
            }

            Console.WriteLine("Done!");
        }