Example #1
0
        /// <summary>
        /// 写日志到指定日志组件
        /// 默认写入Microsoft.Extensions.Logging
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="logMessage">日志消息</param>
        /// <returns></returns>
        protected virtual Task WriteLogAsync(ApiResponseContext context, LogMessage logMessage)
        {
            var logger = context.GetLogger();

            if (logger != null)
            {
                this.WriteLog(logger, logMessage);
            }
            return(Task.CompletedTask);
        }
        /// <summary>
        /// 写日志到LoggerFactory
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="logMessage">日志消息</param>
        /// <returns></returns>
        protected virtual Task WriteLogAsync(ApiResponseContext context, LogMessage logMessage)
        {
            var logger = context.GetLogger();

            if (logger == null)
            {
                return(Task.CompletedTask);
            }

            if (logMessage.Exception == null)
            {
                logger.LogInformation(logMessage.ToString());
            }
            else
            {
                logger.LogError(logMessage.ToString());
            }

            return(Task.CompletedTask);
        }