Example #1
0
 /// <summary>
 /// 重载方法,写入数据
 /// </summary>
 /// <param name="eventCache">包含当前进程 ID、线程 ID 以及堆栈跟踪信息的 TraceEventCache 对象</param>
 /// <param name="source">标识输出时使用的名称,通常为生成跟踪事件的应用程序的名称</param>
 /// <param name="eventType">TraceEventType枚举值,指定引发日志记录的事件类型</param>
 /// <param name="id">事件的数值标识符</param>
 /// <param name="data">要记录的日志数据</param>
 /// <remarks>
 /// <code source="..\Framework\src\DeluxeWorks.Library\Logging\Logger.cs"
 /// lang="cs" region="Process Log" title="写日志"></code>
 /// </remarks>
 public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
 {
     if (data is LogEntity)
     {
         LogEntity logData = data as LogEntity;
         if (this.Formatter != null)
         {
             base.Write(this.Formatter.Format(logData));
         }
         else
         {
             base.TraceData(eventCache, source, eventType, id, logData);
         }
     }
     else
     {
         base.TraceData(eventCache, source, eventType, id, data);
     }
 }
Example #2
0
 /// <summary>
 /// 重载方法,写入数据
 /// </summary>
 /// <param name="eventCache">包含当前进程 ID、线程 ID 以及堆栈跟踪信息的 TraceEventCache 对象</param>
 /// <param name="source">标识输出时使用的名称,通常为生成跟踪事件的应用程序的名称</param>
 /// <param name="logEventType">TraceEventType枚举值,指定引发日志记录的事件类型</param>
 /// <param name="eventID">事件的数值标识符</param>
 /// <param name="data">要记录的日志数据</param>
 /// <remarks>
 /// 只有Formatter属性不为空时,才格式化
 /// <code source="..\Framework\src\DeluxeWorks.Library\Logging\Logger.cs" lang="cs" region="Process Log" title="写日志"></code>
 /// </remarks>
 public override void TraceData(TraceEventCache eventCache, string source, TraceEventType logEventType, int eventID, object data)
 {
     if (data is LogEntity)
     {
         LogEntity logData = data as LogEntity;
         if (this.Formatter != null)
         {
             this.slaveListener.TraceData(eventCache, source, logEventType, eventID, this.Formatter.Format(logData));
         }
         else
         {
             this.slaveListener.TraceData(eventCache, source, logEventType, eventID, logData);
         }
     }
     else
     {
         this.slaveListener.TraceData(eventCache, source, logEventType, eventID, data);
     }
 }
Example #3
0
        /// <summary>
        /// 实现ICloneable接口
        /// </summary>
        /// <returns>
        /// 克隆出的LogEntity对象
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\\Logging\LogEntityTest.cs"
        /// lang="cs" region="LogEntityClone Test" tittle="克隆LogEntity对象"></code>
        /// </returns>
        public object Clone()
        {
            LogEntity log = new LogEntity(this.Message);

            log.EventID      = this.EventID;
            log.Title        = this.Title;
            log.Priority     = this.Priority;
            log.LogEventType = this.LogEventType;
            log.MachineName  = this.MachineName;
            log.StackTrace   = this.StackTrace;
            log.ActivityID   = this.ActivityID;
            log.Source       = this.Source;

            if (this.extendedProperties != null)
            {
                log.extendedProperties = new Dictionary <string, object>(this.extendedProperties);
            }

            return(log);
        }
        /// <summary>
        /// 文本格式化
        /// </summary>
        /// <param name="templateBuilder">包含格式化模板串的StringBuilder</param>
        /// <param name="log">待格式化的LogEntity对象</param>
        /// <returns>格式化成的文本串</returns>
        private string Format(StringBuilder templateBuilder, LogEntity log)
        {
            templateBuilder.Replace(TextLogFormatter.TimeStampToken, log.TimeStamp.ToString("yyyy-MM-dd HH:mm:ss"));
            templateBuilder.Replace(TextLogFormatter.TitleToken, log.Title);
            templateBuilder.Replace(TextLogFormatter.MessageToken, log.Message);
            templateBuilder.Replace(TextLogFormatter.EventIdToken, log.EventID.ToString(Resource.Culture));
            templateBuilder.Replace(TextLogFormatter.EventTypeToken, log.LogEventType.ToString());
            templateBuilder.Replace(TextLogFormatter.StackTraceToken, log.StackTrace);
            templateBuilder.Replace(TextLogFormatter.PriorityToken, log.Priority.ToString());
            templateBuilder.Replace(TextLogFormatter.MachineToken, log.MachineName);

            templateBuilder.Replace(TextLogFormatter.ActivityidToken, log.ActivityID.ToString("D", Resource.Culture));

            FormatTokenFunctions(templateBuilder, log);

            templateBuilder.Replace(TextLogFormatter.NewLineToken, Environment.NewLine);
            templateBuilder.Replace(TextLogFormatter.TabToken, "\t");

            return(templateBuilder.ToString());
        }
        /// <summary>
        /// Searches for token functions in the message and replace all with formatted values.
        /// </summary>
        /// <param name="messageBuilder">Message template containing tokens.</param>
        /// <param name="log">Log entry containing properties to format.</param>
        public virtual void Format(StringBuilder messageBuilder, LogEntity log)
        {
            int pos = 0;

            while (pos < messageBuilder.Length)
            {
                string messageString = messageBuilder.ToString();
                if (messageString.IndexOf(this.startDelimiter) == -1)
                {
                    break;
                }

                string tokenTemplate  = GetInnerTemplate(pos, messageString);
                string tokenToReplace = this.startDelimiter + tokenTemplate + this.endDelimiter;
                pos = messageBuilder.ToString().IndexOf(tokenToReplace);

                string tokenValue = FormatToken(tokenTemplate, log);

                messageBuilder.Replace(tokenToReplace, tokenValue);
            }
        }
        /// <summary>
        /// Formats the timestamp property with the specified date time format string.
        /// </summary>
        /// <param name="tokenTemplate">Date time format string.</param>
        /// <param name="log">Log entry containing the timestamp.</param>
        /// <returns>Returns the formatted time stamp.</returns>
        public override string FormatToken(string tokenTemplate, LogEntity log)
        {
            string result = null;

            if (tokenTemplate.Equals(TimeStampToken.LocalStartDelimiter, System.StringComparison.InvariantCultureIgnoreCase))
            {
                System.DateTime localTime = log.TimeStamp.ToLocalTime();
                result = localTime.ToString();
            }
            else if (tokenTemplate.StartsWith(TimeStampToken.LocalStartDelimiterWithFormat, System.StringComparison.InvariantCultureIgnoreCase))
            {
                string          formatTemplate = tokenTemplate.Substring(TimeStampToken.LocalStartDelimiterWithFormat.Length);
                System.DateTime localTime      = log.TimeStamp.ToLocalTime();
                result = localTime.ToString(formatTemplate, CultureInfo.CurrentCulture);
            }
            else
            {
                result = log.TimeStamp.ToString(tokenTemplate, CultureInfo.CurrentCulture);
            }
            return(result);
        }
Example #7
0
        public void LogOperation(params object[] parameterInputs)
        {
            StackFrame sf     = new StackFrame(1);
            var        method = sf.GetMethod();

            string methodName = method.Name;
            string parameters = string.Join("\r\n", method.GetParameters().Select(
                                                (p, i) =>
                                                string.Format(" {0}({1})={2}", p.Name, p.ParameterType.Name, GetInput(parameterInputs, i)))
                                            .ToArray());

            var genericArgs = method.GetGenericArguments();

            string msg = methodName + "\r\n" + parameters;

            if (HttpContext.Current != null)
            {
                string[] args = { "__1osInfo", "__2browserInfo" };
                foreach (var item in args)
                {
                    msg += "\r\n:" + item + ":" + HttpContext.Current.Items.GetValue(item, "Not registered");
                }
            }


            LogEntity log = new LogEntity(msg)
            {
                Source       = "操作日志",
                Priority     = LogPriority.Lowest,
                EventID      = 0,
                LogEventType = TraceEventType.Information,
                CreatorName  = this.CurrentUserName,
                Title        = "操作日志"
            };

            Write(log);
        }
Example #8
0
        /// <summary>
        /// 重载方法,写入文件
        /// </summary>
        /// <param name="eventCache">包含当前进程 ID、线程 ID 以及堆栈跟踪信息的 TraceEventCache 对象</param>
        /// <param name="source">标识输出时使用的名称,通常为生成跟踪事件的应用程序的名称</param>
        /// <param name="logEventType">TraceEventType枚举值,指定引发日志记录的事件类型</param>
        /// <param name="eventID">事件的数值标识符</param>
        /// <param name="data">要记录的日志数据</param>
        /// <remarks>
        /// <code source="..\Framework\src\DeluxeWorks.Library\Logging\Logger.cs"
        /// lang="cs" region="Process Log" title="写日志"></code>
        /// </remarks>
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType logEventType, int eventID, object data)
        {
            if (data is LogEntity)
            {
                LogEntity logData = data as LogEntity;

                //取LogEntity对象的Source属性,如果为空则取来源于配置的缺省值
                string sourceName = string.IsNullOrEmpty(logData.Source) ? this.source : logData.Source;

                string registerLogName = RegisterSourceToLogName(sourceName, this.logName);

                EventLog eventlog = new EventLog(registerLogName, FormattedEventLogTraceListener.DefaultMachineName, sourceName);

                //if (eventlog.OverflowAction == OverflowAction.OverwriteOlder && eventlog.MinimumRetentionDays > 3)
                //    eventlog.ModifyOverflowPolicy(OverflowAction.OverwriteOlder, 3);

                //if (eventlog.MaximumKilobytes < 1024)
                //    eventlog.MaximumKilobytes = 2048;

                (this.SlaveListener as EventLogTraceListener).EventLog = eventlog;
            }

            base.TraceData(eventCache, source, logEventType, eventID, data);
        }
Example #9
0
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="log">待写的日志记录</param>
        /// <remarks>
        /// 写日志信息的方法
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\\Logging\LoggerTest.cs"
        /// lang="cs" region="Logger Write Test" tittle="写日志信息"></code>
        /// </remarks>
        public void Write(LogEntity log)
        {
            try
            {
                this.rwLock.AcquireReaderLock(Logger.DefaultLockTimeout);

                if (this.enableLog && this.FilterPipeline.IsMatch(log) && ProcessLogHandler != null)
                {
                    ProcessLogHandler.BeginInvoke(log, null, null);
                }
            }
            catch (LogException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new LogException("写日志信息时出错:" + ex.Message, ex);
            }
            finally
            {
                this.rwLock.ReleaseReaderLock();
            }
        }
 /// <summary>
 /// Abstract method to process the token value between the start and end delimiter.
 /// </summary>
 /// <param name="tokenTemplate">Token value between the start and end delimiters.</param>
 /// <param name="log">Log entry to process.</param>
 /// <returns>Formatted value to replace the token.</returns>
 public abstract string FormatToken(string tokenTemplate, LogEntity log);
 /// <summary>
 /// 抽象方法,格式化LogEntity对象成一个字符串
 /// </summary>
 /// <param name="log">待格式化的LogEntity对象</param>
 /// <returns>格式化成的字符串</returns>
 /// <remarks>
 /// 由派生类具体实现
 /// <code source="..\Framework\src\DeluxeWorks.Library\Logging\Formatters\TextFormatter.cs"
 /// lang="cs" region="Format Implementation" title="文本格式化方法"></code>
 /// </remarks>
 public abstract string Format(LogEntity log);
 /// <summary>
 /// 抽象方法,实现日志过滤
 /// </summary>
 /// <param name="log">日志对象</param>
 /// <returns>布尔值,true:通过,false:不通过</returns>
 public abstract bool IsMatch(LogEntity log);
 /// <summary>
 /// 覆写的方法,具体实现优先级过滤
 /// </summary>
 /// <param name="log">日志记录</param>
 /// <returns>布尔值,true:通过,false:不通过</returns>
 /// <remarks>
 /// 只有优先级大于等于minPriority的日志记录才能通过
 /// </remarks>
 public override bool IsMatch(LogEntity log)
 {
     return(log.Priority >= this.minPriority);
 }
 /// <summary>
 /// 文本格式化方法
 /// </summary>
 /// <param name="log">待格式化的LogEntity对象</param>
 /// <returns>格式化成的文本串</returns>
 /// <remarks>
 /// 重载方法,实现文本格式化
 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\\Logging\FormatterTest.cs"
 /// lang="cs" region="Format Test" tittle="获取Formatter对象"></code>
 /// </remarks>
 public override string Format(LogEntity log)
 {
     return(Format(CreateTemplateBuilder(), log));
 }