Exemple #1
0
            internal NLogLogger(object logger)
            {
                var loggerType = logger.GetType();

                _nameDelegate = (NameDelegate)loggerType.GetProperty("Name").GetGetMethod().CreateDelegate(typeof(NameDelegate), logger);

                var logEventInfoType = FindType("NLog.LogEventInfo", "NLog");

                _logEventDelegate = (type, e) => loggerType.GetMethod("Log", new Type[] { typeof(Type), logEventInfoType }).Invoke(logger, new object[] { type, e });

                _isTraceEnabledDelegate = GetIsEnabledDelegate(logger, "IsTraceEnabled");
                _isDebugEnabledDelegate = GetIsEnabledDelegate(logger, "IsDebugEnabled");
                _isInfoEnabledDelegate  = GetIsEnabledDelegate(logger, "IsInfoEnabled");
                _isWarnEnabledDelegate  = GetIsEnabledDelegate(logger, "IsWarnEnabled");
                _isErrorEnabledDelegate = GetIsEnabledDelegate(logger, "IsErrorEnabled");
                _isFatalEnabledDelegate = GetIsEnabledDelegate(logger, "IsFatalEnabled");

                _traceDelegate = GetLogDelegate(logger, "Trace");
                _debugDelegate = GetLogDelegate(logger, "Debug");
                _infoDelegate  = GetLogDelegate(logger, "Info");
                _warnDelegate  = GetLogDelegate(logger, "Warn");
                _errorDelegate = GetLogDelegate(logger, "Error");
                _fatalDelegate = GetLogDelegate(logger, "Fatal");

                _traceExceptionDelegate = GetLogExceptionDelegate(logger, "TraceException");
                _debugExceptionDelegate = GetLogExceptionDelegate(logger, "DebugException");
                _infoExceptionDelegate  = GetLogExceptionDelegate(logger, "InfoException");
                _warnExceptionDelegate  = GetLogExceptionDelegate(logger, "WarnException");
                _errorExceptionDelegate = GetLogExceptionDelegate(logger, "ErrorException");
                _fatalExceptionDelegate = GetLogExceptionDelegate(logger, "FatalException");
            }
Exemple #2
0
 /// <summary>
 /// 初始化 Log 工具(建议)
 /// </summary>
 /// <param name="console">控制台输出级别</param>
 /// <param name="file">文件输出级别</param>
 /// <param name="action">日志输出回调</param>
 public Log(LogLevel console = LogLevel.All, LogLevel file = LogLevel.All, LogEventDelegate logEvent = null)
 {
     ConsoleLogLevel = console;
     FileLogLevel    = file;
     if (logEvent != null)
     {
         LogEvent += logEvent;
     }
 }
Exemple #3
0
        public Form1()
        {
            InitializeComponent();
            LogEventDelegate logEventDelegate = new LogEventDelegate();

            logEventDelegate.Logging += WriteToRtb1;
            Config.Load();
            //Log.InitLogs(rtb1);
            Log.InitLogs(logEventDelegate);
        }
 public void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.CategoryCommands, "{Account} deleted the category {Category}", this.AccountName, this.CategoryName);
     }
     else
     {
         log(Events.CategoryCommands, "Deleting the category {Category} for {Account} failed", this.CategoryName, this.AccountName);
     }
 }
Exemple #5
0
 public void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.UserCommands, "{Username} changed the password", this.User.Name);
     }
     else
     {
         log(Events.UserCommands, "Changing the password for {Username} failed", this.User.Name);
     }
 }
Exemple #6
0
 public void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.LoginCommand, "{Username} logged in", Username);
     }
     else
     {
         log(Events.LoginCommand, "Login for {Username} failed", this.Username);
     }
 }
 public void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.CategoryCommands, "{Account} deleted expense {ExpenseId}", this.AccountName, this.Id);
     }
     else
     {
         log(Events.CategoryCommands, "Deleting expense {ExpenseId} for {Account} failed", this.Id, this.AccountName);
     }
 }
Exemple #8
0
 public void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.RefreshTokenCommand, "{Username} refreshed the token", User.Name);
     }
     else
     {
         log(Events.RefreshTokenCommand, "Token refresh for {Username} failed", this.User.Name);
     }
 }
Exemple #9
0
 public void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.CategoryCommands, "{Account} created a new subcategory {Category}/{Subcategory}", this.AccountName, this.CategoryName, this.SubcategoryName);
     }
     else
     {
         log(Events.CategoryCommands, "Creating a new subcategory {Category}/{Subcategory} for {Account} failed", this.CategoryName, this.SubcategoryName, this.AccountName);
     }
 }
 public void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.AdminCommands, "Admin created the user {Username}", this.Name);
     }
     else
     {
         log(Events.AdminCommands, "Creating the user {Username} failed", this.Name);
     }
 }
Exemple #11
0
 public void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.HandleImportedExpenseCommand, "{Account} linked an imported expense", this.AccountName);
     }
     else
     {
         log(Events.HandleImportedExpenseCommand, "Linking an imported expense for {Account} failed", this.AccountName);
     }
 }
Exemple #12
0
 public virtual void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.ImportCsvFile, "{Account} imported a csv file", this.AccountName);
     }
     else
     {
         log(Events.ImportCsvFile, "Import a csv file for {Account} failed", this.AccountName);
     }
 }
 public virtual void EmitLog(LogEventDelegate log, bool success)
 {
     if (success)
     {
         log(Events.ExpenseCommands, "{Account} created a new {ExpenseType}", this.AccountName, this.Type.ToString());
         log(Events.HandleImportedExpenseCommand, "{Account} handled an imported expense", this.AccountName);
     }
     else
     {
         log(Events.ExpenseCommands, "Creating a new {ExpenseType} for {Account} failed", this.Type.ToString(), this.AccountName);
         log(Events.HandleImportedExpenseCommand, "Handling an imported expense for {Account} failed", this.AccountName);
     }
 }
 void LogEvent(string message)
 {
     if (InvokeRequired)
     {
         LogEventDelegate d = new LogEventDelegate(LogEvent);
         this.Invoke(d, new object[] { message });
     }
     else
     {
         ListViewItem lvi = new ListViewItem(DateTime.Now.ToLongTimeString());
         lvi.SubItems.Add(message);
         lvLog.Items.Add(lvi);
         lvLog.EnsureVisible(lvLog.Items.Count - 1);
     }
 }
        public EventSourceTest(
            string name,
            LogEventDelegate logEvent,
            VerifyEventDelegate verifyEvent)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (logEvent == null)
            {
                throw new ArgumentNullException(nameof(logEvent));
            }
            if (verifyEvent == null)
            {
                throw new ArgumentNullException(nameof(verifyEvent));
            }

            m_name        = name;
            m_logEvent    = logEvent;
            m_verifyEvent = verifyEvent;
        }
 public void RegisterEvent(DataRecvEventDelegate _SEventRecv, CloseEventDelegate _SEventClose,
                           LogEventDelegate _LogEventSend, LogEventDelegate _LogEventRecv)
 {
     SEventRecv = _SEventRecv;
     SEventClose = _SEventClose;
     LogEventSend = _LogEventSend;
     LogEventRecv = _LogEventRecv;
 }
Exemple #17
0
            private static bool Initialize()
            {
                try
                {
                    var logEventLevelType = FindType("NLog.LogLevel", "NLog");
                    if (logEventLevelType == null)
                    {
                        throw new LibLogException("Type NLog.LogLevel was not found.");
                    }

                    var levelFields = logEventLevelType.GetFields().ToList();
                    s_levelTrace = levelFields.First(x => x.Name == "Trace").GetValue(null);
                    s_levelDebug = levelFields.First(x => x.Name == "Debug").GetValue(null);
                    s_levelInfo  = levelFields.First(x => x.Name == "Info").GetValue(null);
                    s_levelWarn  = levelFields.First(x => x.Name == "Warn").GetValue(null);
                    s_levelError = levelFields.First(x => x.Name == "Error").GetValue(null);
                    s_levelFatal = levelFields.First(x => x.Name == "Fatal").GetValue(null);

                    var logEventInfoType = FindType("NLog.LogEventInfo", "NLog");
                    if (logEventInfoType == null)
                    {
                        throw new LibLogException("Type NLog.LogEventInfo was not found.");
                    }

                    var loggingEventConstructor =
                        logEventInfoType.GetConstructorPortable(logEventLevelType, typeof(string),
                                                                typeof(IFormatProvider), typeof(string), typeof(object[]), typeof(Exception));

                    var loggerNameParam  = Expression.Parameter(typeof(string));
                    var levelParam       = Expression.Parameter(typeof(object));
                    var messageParam     = Expression.Parameter(typeof(string));
                    var messageArgsParam = Expression.Parameter(typeof(object[]));
                    var exceptionParam   = Expression.Parameter(typeof(Exception));
                    var levelCast        = Expression.Convert(levelParam, logEventLevelType);

                    var newLoggingEventExpression =
                        Expression.New(loggingEventConstructor,
                                       levelCast,
                                       loggerNameParam,
                                       Expression.Constant(null, typeof(IFormatProvider)),
                                       messageParam,
                                       messageArgsParam,
                                       exceptionParam
                                       );

                    s_logEventInfoFact = Expression.Lambda <Func <string, object, string, object[], Exception, object> >(
                        newLoggingEventExpression,
                        loggerNameParam, levelParam, messageParam, messageArgsParam, exceptionParam).Compile();

                    var loggerType = FindType("NLog.Logger", "NLog");

                    s_loggerNameDelegate = GetLoggerNameDelegate(loggerType);

                    s_logEventDelegate = GetLogEventDelegate(loggerType, logEventInfoType);

                    s_isTraceEnabledDelegate = GetIsEnabledDelegate(loggerType, "IsTraceEnabled");
                    s_isDebugEnabledDelegate = GetIsEnabledDelegate(loggerType, "IsDebugEnabled");
                    s_isInfoEnabledDelegate  = GetIsEnabledDelegate(loggerType, "IsInfoEnabled");
                    s_isWarnEnabledDelegate  = GetIsEnabledDelegate(loggerType, "IsWarnEnabled");
                    s_isErrorEnabledDelegate = GetIsEnabledDelegate(loggerType, "IsErrorEnabled");
                    s_isFatalEnabledDelegate = GetIsEnabledDelegate(loggerType, "IsFatalEnabled");

                    s_traceDelegate = GetLogDelegate(loggerType, "Trace");
                    s_debugDelegate = GetLogDelegate(loggerType, "Debug");
                    s_infoDelegate  = GetLogDelegate(loggerType, "Info");
                    s_warnDelegate  = GetLogDelegate(loggerType, "Warn");
                    s_errorDelegate = GetLogDelegate(loggerType, "Error");
                    s_fatalDelegate = GetLogDelegate(loggerType, "Fatal");

                    s_traceExceptionDelegate = GetLogExceptionDelegate(loggerType, "TraceException");
                    s_debugExceptionDelegate = GetLogExceptionDelegate(loggerType, "DebugException");
                    s_infoExceptionDelegate  = GetLogExceptionDelegate(loggerType, "InfoException");
                    s_warnExceptionDelegate  = GetLogExceptionDelegate(loggerType, "WarnException");
                    s_errorExceptionDelegate = GetLogExceptionDelegate(loggerType, "ErrorException");
                    s_fatalExceptionDelegate = GetLogExceptionDelegate(loggerType, "FatalException");

                    s_structuredLoggingEnabled = IsStructuredLoggingEnabled();
                }
                catch (Exception ex)
                {
                    s_initializeException = ex;
                    return(false);
                }

                return(true);
            }
Exemple #18
0
            static NLogLogger()
            {
                try
                {
                    var logEventLevelType = Type.GetType("NLog.LogLevel, NLog");
                    if (logEventLevelType == null)
                    {
                        throw new InvalidOperationException("Type NLog.LogLevel was not found.");
                    }

                    var levelFields = logEventLevelType.GetFields().ToList();
                    _levelTrace = levelFields.First(x => x.Name == "Trace").GetValue(null);
                    _levelDebug = levelFields.First(x => x.Name == "Debug").GetValue(null);
                    _levelInfo  = levelFields.First(x => x.Name == "Info").GetValue(null);
                    _levelWarn  = levelFields.First(x => x.Name == "Warn").GetValue(null);
                    _levelError = levelFields.First(x => x.Name == "Error").GetValue(null);
                    _levelFatal = levelFields.First(x => x.Name == "Fatal").GetValue(null);

                    var logEventInfoType = Type.GetType("NLog.LogEventInfo, NLog");
                    if (logEventInfoType == null)
                    {
                        throw new InvalidOperationException("Type NLog.LogEventInfo was not found.");
                    }

                    var loggingEventConstructor = logEventInfoType.GetConstructor(new []
                    {
                        logEventLevelType,
                        typeof(string),
                        typeof(IFormatProvider),
                        typeof(string),
                        typeof(object[]),
                        typeof(Exception),
                    });
                    ParameterExpression loggerNameParam  = Expression.Parameter(typeof(string));
                    ParameterExpression levelParam       = Expression.Parameter(typeof(object));
                    ParameterExpression messageParam     = Expression.Parameter(typeof(string));
                    ParameterExpression messageArgsParam = Expression.Parameter(typeof(object[]));
                    ParameterExpression exceptionParam   = Expression.Parameter(typeof(Exception));
                    UnaryExpression     levelCast        = Expression.Convert(levelParam, logEventLevelType);

                    NewExpression newLoggingEventExpression =
                        Expression.New(loggingEventConstructor,
                                       levelCast,
                                       loggerNameParam,
                                       Expression.Constant(null, typeof(IFormatProvider)),
                                       messageParam,
                                       messageArgsParam,
                                       exceptionParam
                                       );

                    _logEventInfoFact = Expression.Lambda <Func <string, object, string, object[], Exception, object> >(
                        newLoggingEventExpression,
                        loggerNameParam, levelParam, messageParam, messageArgsParam, exceptionParam).Compile();

                    Type loggerType = Type.GetType("NLog.Logger, NLog");

                    _loggerNameDelegate = GetLoggerNameDelegate(loggerType);

                    _logEventDelegate = GetLogEventDelegate(loggerType, logEventInfoType);

                    _isTraceEnabledDelegate = GetIsEnabledDelegate(loggerType, "IsTraceEnabled");
                    _isDebugEnabledDelegate = GetIsEnabledDelegate(loggerType, "IsDebugEnabled");
                    _isInfoEnabledDelegate  = GetIsEnabledDelegate(loggerType, "IsInfoEnabled");
                    _isWarnEnabledDelegate  = GetIsEnabledDelegate(loggerType, "IsWarnEnabled");
                    _isErrorEnabledDelegate = GetIsEnabledDelegate(loggerType, "IsErrorEnabled");
                    _isFatalEnabledDelegate = GetIsEnabledDelegate(loggerType, "IsFatalEnabled");

                    _traceDelegate = GetLogDelegate(loggerType, "Trace");
                    _debugDelegate = GetLogDelegate(loggerType, "Debug");
                    _infoDelegate  = GetLogDelegate(loggerType, "Info");
                    _warnDelegate  = GetLogDelegate(loggerType, "Warn");
                    _errorDelegate = GetLogDelegate(loggerType, "Error");
                    _fatalDelegate = GetLogDelegate(loggerType, "Fatal");

                    _traceExceptionDelegate = GetLogExceptionDelegate(loggerType, "TraceException");
                    _debugExceptionDelegate = GetLogExceptionDelegate(loggerType, "DebugException");
                    _infoExceptionDelegate  = GetLogExceptionDelegate(loggerType, "InfoException");
                    _warnExceptionDelegate  = GetLogExceptionDelegate(loggerType, "WarnException");
                    _errorExceptionDelegate = GetLogExceptionDelegate(loggerType, "ErrorException");
                    _fatalExceptionDelegate = GetLogExceptionDelegate(loggerType, "FatalException");

                    _structuredLoggingEnabled = IsStructuredLoggingEnabled();
                }
                catch { }
            }