public void AddItem(LogItem item)
        {
            if (LogLevel == LogLevel.None || item == null || string.IsNullOrWhiteSpace(item.Exception.ToString()))
            {
                return;
            }

            try
            {
                OnItemAdded.RaiseEvent(item, new EventArgs());

                var text = item.Exception.ToString();
                text = item.Exception.Data.Cast<DictionaryEntry>()
                    .Aggregate(
                        text,
                        (current, entry) =>
                            current + string.Format("{0}{1}: {2}", Environment.NewLine, entry.Key, entry.Value));


                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(text);
                Console.ResetColor();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        public void AddItem(LogItem item)
        {
            if (LogLevel == LogLevel.None || item == null || string.IsNullOrWhiteSpace(item.Exception.ToString()))
            {
                return;
            }

            try
            {
                var uniqueValue = (item.Exception + AdditionalData.ToDebugString()).Trim();
                if (!_unique.Contains(uniqueValue))
                {
                    OnItemAdded.RaiseEvent(item, new EventArgs());
                    _unique.Add(uniqueValue);

                    var file = Path.Combine(
                        LogDir,
                        string.Format(
                            _fileName, DateTime.Now.ToString("yyyy_MM_dd"), LogLevel.ToString().ToLower(),
                            (item.Exception + AdditionalData.ToDebugString()).ToMd5Hash()));

                    if (File.Exists(file))
                    {
                        return;
                    }

                    if (OutputConsole)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(item.Exception);
                        Console.ResetColor();
                    }

                    using (
                        var fileStream = new FileStream(
                            file, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, true))
                    {
                        using (Stream gzStream = new GZipStream(fileStream, CompressionMode.Compress, false))
                        {
                            var text = item.Exception.ToString();
                            text = item.Exception.Data.Cast<DictionaryEntry>()
                                .Aggregate(
                                    text,
                                    (current, entry) =>
                                        current +
                                        string.Format("{0}{1}: {2}", Environment.NewLine, entry.Key, entry.Value));

                            if (string.IsNullOrWhiteSpace(text.Trim()))
                            {
                                return;
                            }

                            var logByte = new UTF8Encoding(true).GetBytes(text);

                            if (Compression)
                            {
                                gzStream.Write(logByte, 0, logByte.Length);
                            }
                            else
                            {
                                fileStream.Write(logByte, 0, logByte.Length);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Пример #3
0
        public void AddItem(LogItem item)
        {
            if (LogLevel == LogLevel.None || item == null || string.IsNullOrWhiteSpace(item.Exception.ToString()))
            {
                return;
            }

            try
            {
                var uniqueValue = (item.Exception + AdditionalData.ToDebugString()).Trim();
                if (!_unique.Contains(uniqueValue))
                {
                    OnItemAdded.RaiseEvent(item, new EventArgs());
                    _unique.Add(uniqueValue);

                    var file = Path.Combine(
                        LogDir,
                        string.Format(
                            _fileName, DateTime.Now.ToString("yyyy_MM_dd"), LogLevel.ToString().ToLower(),
                            (item.Exception + AdditionalData.ToDebugString()).ToMd5Hash()));

                    if (File.Exists(file))
                    {
                        return;
                    }

                    AddData(item.Exception);

                    if (OutputConsole)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(item.Exception);
                        Console.ResetColor();
                    }

                    using (
                        var fileStream = new FileStream(
                            file, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, true))
                    {
                        using (Stream gzStream = new GZipStream(fileStream, CompressionMode.Compress, false))
                        {
                            var text = item.Exception.ToString();
                            text = item.Exception.Data.Cast <DictionaryEntry>()
                                   .Aggregate(
                                text,
                                (current, entry) =>
                                current +
                                string.Format("{0}{1}: {2}", Environment.NewLine, entry.Key, entry.Value));

                            if (string.IsNullOrWhiteSpace(text.Trim()))
                            {
                                return;
                            }

                            var logByte = new UTF8Encoding(true).GetBytes(text);

                            if (Compression)
                            {
                                gzStream.Write(logByte, 0, logByte.Length);
                            }
                            else
                            {
                                fileStream.Write(logByte, 0, logByte.Length);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }