Example #1
0
        /// <summary>
        /// Inserts a log item
        /// </summary>
        /// <param name="logLevel">Log level</param>
        /// <param name="shortMessage">The short message</param>
        /// <param name="fullMessage">The full message</param>
        /// <param name="exception">The error associated with this log</param>
        /// <param name="user">The user to associate log record with</param>
        /// <param name="requestContext">The request context. If this is supplied, additional info will be logged, like ip, post/server variables, etc</param>
        /// <returns>A log item</returns>
        public Log InsertLog(LogLevel logLevel, string shortMessage, string fullMessage = "", Exception exception = null, string user = null)
        {
            Exception ex = null;

            if (exception != null)
            {
                if (IsBuiltInException(exception))
                {
                    ex = exception.GetBaseException();
                }
            }

            var log = new Log()
            {
                LogLevel     = logLevel,
                ShortMessage = shortMessage ?? string.Empty,
                FullMessage  = fullMessage ?? string.Empty,
                User         = user,
                CreatedOnUtc = CommonHelper.CurrentTime()
            };

            if (ex != null)
            {
                if (string.IsNullOrEmpty(shortMessage))
                {
                    log.ShortMessage = ex.Message;
                }
                else
                {
                    log.ShortMessage += Environment.NewLine + ex.Message;
                }

                log.FullMessage += Environment.NewLine + "Full Trace:" + Environment.NewLine + ex.StackTrace;
            }

            foreach (var customData in LogStore.GetCustomData(ex ?? new LogException(logLevel, shortMessage, fullMessage), _container))
            {
                log.CustomData.Add(customData.Key, customData.Value);
            }

            _logCollection.Insert(log);

            return(log);
        }