コード例 #1
0
        ///添加
        private void AddCollMess(EntityMessages message)
        {
            if (!blockColl.IsAddingCompleted)
            {
                blockColl.Add(message);
            }
            else
            {
                var task = _kafkaService.Producer(_logStoreTopic
                                                  , message.Id, message);
                if (task == null)
                {
                    throw new NullReferenceException("方法没有返回有效的task");
                }

                //System.Diagnostics.Debug.WriteLine("即将执行kafka日志Producer");
                var result = task.Result;
                //Console.WriteLine(_prix+"--"+ logBuilder.ToString());
            }
        }
コード例 #2
0
        public virtual void WriteMessage(LogLevel logLevel, string logName, int eventId, string message, Exception exception)
        {
            var logBuilder = _logBuilder;

            _logBuilder = null;

            if (logBuilder == null)
            {
                logBuilder = new StringBuilder();
            }

            var logLevelString = string.Empty;

            // Example:
            // INFO: ConsoleApp.Program[10]
            //       Request received

            logLevelString = GetLogLevelString(logLevel);
            // category and event id
            logBuilder.Append(_loglevelPadding);
            logBuilder.Append(logName);
            logBuilder.Append("[");
            logBuilder.Append(eventId);
            logBuilder.AppendLine("]");

            // scope information
            GetScopeInformation(logBuilder);

            if (!string.IsNullOrEmpty(message))
            {
                // message
                logBuilder.Append(_messagePadding);

                var len = logBuilder.Length;
                logBuilder.AppendLine(message);
                logBuilder.Replace(Environment.NewLine, _newLineWithMessagePadding, len, message.Length);
            }

            // Example:
            // System.InvalidOperationException
            //    at Namespace.Class.Function() in File:line X
            if (exception != null)
            {
                // exception message
                logBuilder.AppendLine(exception.ToString());
            }

            var hasLevel = !string.IsNullOrEmpty(logLevelString);
            // Queue log message
            string machineName   = Environment.MachineName;
            var    entityMessage = new EntityMessages()
            {
                Id          = Guid.NewGuid().ToString("N"),
                MachineName = machineName,
                OtherFlag   = _prix,
                DateTime    = DateTime.Now,
                LogLevel    = (hasLevel ? logLevelString : null),
                Message     = logBuilder.ToString()
            };

            AddCollMess(entityMessage);

            logBuilder.Clear();
            if (logBuilder.Capacity > 1024)
            {
                logBuilder.Capacity = 1024;
            }
            _logBuilder = logBuilder;
        }