示例#1
0
        /// <summary>
        ///
        /// </summary>
        public void Create(AppLogInput input)
        {
            var appLog = BindToDomainModel(input);

            _appLogs.Add(appLog);
            _uow.SaveChanges();
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        public async Task CreateAsync(AppLogInput input)
        {
            var appLog = BindToDomainModel(input);

            await _appLogs.AddAsync(appLog);

            await _uow.SaveChangesAsync();
        }
示例#3
0
 /// <summary>
 ///
 /// </summary>
 private AppLog BindToDomainModel(AppLogInput appLog)
 {
     return(new AppLog
     {
         Logger = appLog.Logger,
         LogLevel = appLog.LogLevel,
         Message = appLog.Message,
         Url = appLog.Url,
         ApplicationName = appLog.ApplicationName,
     });
 }
示例#4
0
        public async Task <IActionResult> Index()
        {
            var log = new AppLogInput
            {
                ApplicationName = "DbLogger.Core.Example",
                LogLevel        = LogLevel.Information,
                Message         = "I See Home => Index",
            };
            await _appLogService.CreateAsync(log);

            return(View());
        }
        /// <summary>
        ///
        /// </summary>
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            var message = formatter(state, exception);

            if (exception != null)
            {
                message = $"{message}{Environment.NewLine}{exception}";
            }

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            var httpContextAccessor = _serviceProvider.GetRequiredService <IHttpContextAccessor>();
            var appLogItem          = new AppLogInput
            {
                Url = httpContextAccessor?.HttpContext != null?httpContextAccessor.HttpContext.Request.Path.ToString() : string.Empty,
                          LogLevel        = logLevel,
                          Logger          = _loggerName,
                          Message         = message,
                          ApplicationName = _options.ApplicationName,
            };


            try
            {
                using (var serviceScope = _serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
                {
                    var context            = serviceScope.ServiceProvider.GetRequiredService <ILoggerUnitOfWork>();
                    var appLogItemsService = serviceScope.ServiceProvider.GetRequiredService <IAppLogService>();

                    //create log
                    appLogItemsService.Create(appLogItem);
                }
            }
            catch
            {
                // don't throw exceptions from logger
            }
        }
        public void Can_Create_Log()
        {
            RunScopedService <IAppLogService>(ServiceProvider, async appLogItemService =>
            {
                //Arrange
                var model = new AppLogInput
                {
                    Url             = "/test",
                    LogLevel        = LogLevel.Error,
                    Logger          = "Test Logger",
                    Message         = "Test Message",
                    ApplicationName = "Logging.Tests",
                };


                //Act
                await appLogItemService.CreateAsync(model);
                var appLogItem = await appLogItemService.GetAsync(1);

                //Assert
                Assert.AreEqual(model.ApplicationName, appLogItem.ApplicationName);
            });
        }