コード例 #1
0
        public static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .AddCommandLine(args)

                         // Without *some* values in the config, we get a null instance of `ProgramConfig`
                         // however if we ensure at least one value is in the config collection, then we get
                         // an instance, no matter what the key/value is.
                         .AddInMemoryCollection(initialData: new Dictionary <string, string> {
                { "dummy", "value" },
            })
                         .Build()
                         .Get <ProgramConfig>();

            IAudit audit = new SerilogSeqAuditClient(
                new SerilogSeqAuditClientConfiguration
            {
                ServerUrl            = config.Ingestion.Endpoint,
                ApiKey               = config.Ingestion.ApiKey,
                EnrichFromLogContext = config.Client.EnrichFromLogContext,
            });

            var name    = typeof(Program).Assembly.GetName().Name;
            var version = typeof(Program).Assembly.GetName().Version;

            Console.WriteLine("Sending audit event to {0}", config.Ingestion.Endpoint);

            using (Serilog.Context.LogContext.PushProperty("LogContext1", Guid.NewGuid()))
            {
                audit
                .ForContext("AuditForContext1", Guid.NewGuid())
                .ForContext("StartArgs", args)
                .Write("Hello from {Application} v{Version}", name, version);
            }
        }
コード例 #2
0
        public async Task SendsAPostHttpRequestWithTheCorrectCompactJsonDataImmediately()
        {
            HttpRequestMessage receivedHttpRequest = null;
            var messageHandler = new CustomHttpMessageHandler(request =>
            {
                receivedHttpRequest = request;
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.Created)));
            });

            var client = new SerilogSeqAuditClient(
                new SerilogSeqAuditClientConfiguration
            {
                ServerUrl            = new Uri("http://localhost:5341/"),
                ApiKey               = null,
                EnrichFromLogContext = true,
            }, messageHandler: messageHandler);

            using (LogContext.PushProperty("LogContext", "success"))
            {
                client
                .ForContext("ForContext", "success")
                .Write("Hey hey, from {Source}", nameof(AuditTests));
            }

            Assert.NotNull(receivedHttpRequest);
            Assert.Equal(HttpMethod.Post, receivedHttpRequest.Method);
            Assert.Equal(new Uri("http://localhost:5341/api/events/raw"), receivedHttpRequest.RequestUri);
            var requestBodyContent = await receivedHttpRequest.Content.ReadAsStringAsync().ConfigureAwait(false);

            this.output.WriteLine(requestBodyContent);

            using (var clefReader =
                       new Serilog.Formatting.Compact.Reader.LogEventReader(new StringReader(requestBodyContent)))
            {
                var didRead = clefReader.TryRead(out var deserializedEvent);
                Assert.True(didRead);

                Assert.Equal(LogEventLevel.Information, deserializedEvent.Level);
                Assert.Null(deserializedEvent.Exception);
                Assert.Equal("Hey hey, from {Source}", deserializedEvent.MessageTemplate.Text);

                Assert.Collection(
                    deserializedEvent.Properties,
                    item => Assert.Equal("Source", item.Key),
                    item => Assert.Equal("ForContext", item.Key),
                    item => Assert.Equal("LogContext", item.Key));
            }
        }