Beispiel #1
0
        public static async Task <int?> Log(
            ApplicationLog.LogTypes type,
            string message,
            string source           = null,
            string data             = null,
            string stackTrace       = null,
            string userId           = null,
            HttpContext httpContext = null)
        {
            var log = new CreateApplicationLogInputDto
            {
                Type       = type,
                Message    = message,
                Source     = source,
                Data       = data,
                StackTrace = stackTrace,
                UserId     = userId
            };

            if (httpContext != null)
            {
                log.AddHttpContextInformation(httpContext);
            }

            return(await Log(log));
        }
Beispiel #2
0
        public static async Task <int?> Log(CreateApplicationLogInputDto log)
        {
            var aggregate = new CreateApplicationLogCommandAggregate(log);

            await aggregate.SaveAsync();

            return(aggregate.RootEntity.Id);
        }
        public async Task TestLogging()
        {
            var log = new CreateApplicationLogInputDto
            {
                Type    = ApplicationLog.LogTypes.Error,
                Message = "Test log message",
                UserId  = 1.ToString()
            };

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Headers["user-agent"] = "test user agent";

            httpContext.Request.Scheme = "http";

            httpContext.Request.Host = new HostString("localhost", 8080);

            httpContext.Request.Path = "/path";

            httpContext.Request.QueryString = new QueryString("?query=some");

            httpContext.Connection.RemoteIpAddress = IPAddress.Parse("72.43.15.36");

            httpContext.Connection.LocalIpAddress = IPAddress.Parse("192.168.1.42");

            log.AddHttpContextInformation(httpContext);

            await ApplicationLogger.Log(log);

            var(count, logs) = await ApplicationLogger.Get(null);

            var l = logs.Single();

            Assert.AreEqual(ApplicationLog.LogTypes.Error, l.Type);

            Assert.AreEqual("Test log message", l.Message);

            Assert.AreEqual("1", l.UserId);

            Assert.AreEqual("test user agent", l.UserAgent);

            Assert.AreEqual("http://localhost:8080/path?query=some", l.Url);

            Assert.AreEqual("72.43.15.36", l.UserIpAddress);

            Assert.AreEqual("192.168.1.42", l.HostIpAddress);

            await ApplicationLogger.DeleteLogs(DateTime.Now);

            (count, logs) = await ApplicationLogger.Get(null);

            // Logs are deleted for fractions of a second after created
            Assert.AreEqual(0, logs.Count());
        }
        public static void AddHttpContextInformation(this CreateApplicationLogInputDto log, HttpContext httpContext)
        {
            var request = httpContext.Request;

            log.UserAgent = request.Headers["user-agent"].FirstOrDefault();

            var connection = httpContext.Features.Get <IHttpConnectionFeature>();

            log.UserIpAddress = connection?.RemoteIpAddress?.ToString();

            log.HostIpAddress = connection?.LocalIpAddress?.ToString();

            log.Url = new UriBuilder
            {
                Scheme = request.Scheme,
                Host   = request.Host.Host,
                Port   = request.Host.Port.GetValueOrDefault(80),
                Path   = request.Path.ToString(),
                Query  = request.QueryString.ToString()
            }
            .ToString();
        }