示例#1
0
 private AnalogyLogMessage ParserJObject(JObject jo, ILogger analogy)
 {
     var evt = LogEventReader.ReadFromJObject(jo);
     {
         analogy.Write(evt);
         return(CommonParser.ParseLogEventProperties(evt));
     }
 }
示例#2
0
        public async Task <IEnumerable <AnalogyLogMessage> > Process(string fileName, CancellationToken token, ILogMessageCreatedHandler messagesHandler)
        {
            var messages = await Task <IEnumerable <AnalogyLogMessage> > .Factory.StartNew(() =>
            {
                List <AnalogyLogMessage> parsedMessages = new List <AnalogyLogMessage>();
                try
                {
                    using (var analogy = new LoggerConfiguration()
                                         .WriteTo.Analogy()
                                         .CreateLogger())
                    {
                        using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            using (var clef = new StreamReader(fileStream, encoding: Encoding.UTF8))
                            {
                                var reader = new LogEventReader(clef);
                                while (reader.TryRead(out var evt))
                                {
                                    analogy.Write(evt);
                                    AnalogyLogMessage m = CommonParser.ParseLogEventProperties(evt);
                                    if (TimeStampTimeZone != null)
                                    {
                                        m.Date = TimeZoneInfo.ConvertTimeFromUtc(m.Date, TimeStampTimeZone);
                                    }
                                    parsedMessages.Add(m);
                                }

                                messagesHandler.AppendMessages(parsedMessages, fileName);
                                return(parsedMessages);
                            }
                    }
                }
                catch (Exception e)
                {
                    AnalogyLogMessage empty = new AnalogyLogMessage($"Error reading file {fileName}: Error: {e.Message}",
                                                                    AnalogyLogLevel.Error, AnalogyLogClass.General, "Analogy", "None");
                    empty.Source = nameof(ClefParser);
                    empty.Module = "Analogy.LogViewer.Serilog";
                    parsedMessages.Add(empty);
                    messagesHandler.AppendMessages(parsedMessages, fileName);
                    return(parsedMessages);
                }
            });

            return(messages);
        }
        public async Task <IEnumerable <AnalogyLogMessage> > Process(string fileName, CancellationToken token, ILogMessageCreatedHandler messagesHandler)
        {
            var messages = await Task <IEnumerable <AnalogyLogMessage> > .Factory.StartNew(() =>
            {
                List <AnalogyLogMessage> parsedMessages = new List <AnalogyLogMessage>();
                try
                {
                    using (var analogy = new LoggerConfiguration()
                                         .WriteTo.Analogy()
                                         .CreateLogger())
                    {
                        using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            string jsonData;
                            if (fileName.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase))
                            {
                                using (var gzStream = new GZipStream(fileStream, CompressionMode.Decompress))
                                {
                                    using (var streamReader = new StreamReader(gzStream, encoding: Encoding.UTF8))
                                    {
                                        jsonData = streamReader.ReadToEnd();
                                    }
                                }
                            }
                            else
                            {
                                using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
                                {
                                    jsonData = streamReader.ReadToEnd();
                                }
                            }


                            var data = JsonConvert.DeserializeObject(jsonData);
                            if (data is JObject jo)
                            {
                                var evt = LogEventReader.ReadFromJObject(jo, messageFields);
                                analogy.Write(evt);
                                AnalogyLogMessage m = CommonParser.ParseLogEventProperties(evt);
                                parsedMessages.Add(m);
                            }
                            else if (data is JArray arr)
                            {
                                foreach (var obj in arr.ToList())
                                {
                                    if (obj is JObject j)
                                    {
                                        var evt = LogEventReader.ReadFromJObject(j, messageFields);
                                        analogy.Write(evt);
                                        AnalogyLogMessage m = CommonParser.ParseLogEventProperties(evt);
                                        parsedMessages.Add(m);
                                    }
                                }
                            }
                        }
                    }

                    messagesHandler.AppendMessages(parsedMessages, fileName);
                    return(parsedMessages);
                }

                catch (Exception e)
                {
                    AnalogyLogMessage empty = new AnalogyLogMessage($"Error reading file {fileName}: Error: {e.Message}",
                                                                    AnalogyLogLevel.Error, AnalogyLogClass.General, "Analogy", "None");
                    empty.Source = nameof(CompactJsonFormatParser);
                    empty.Module = "Analogy.LogViewer.Serilog";
                    parsedMessages.Add(empty);
                    messagesHandler.AppendMessages(parsedMessages, fileName);
                    return(parsedMessages);
                }
            });

            return(messages);
        }
示例#4
0
        public async Task <IEnumerable <AnalogyLogMessage> > Process(string fileName, CancellationToken token,
                                                                     ILogMessageCreatedHandler messagesHandler)
        {
            //var formatter = new JsonFormatter();
            List <AnalogyLogMessage> parsedMessages = new List <AnalogyLogMessage>();

            try
            {
                using (var analogy = new LoggerConfiguration().WriteTo.Analogy()
                                     .CreateLogger())
                {
                    using (var fileStream =
                               new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        if (fileName.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase))
                        {
                            using (var gzStream = new GZipStream(fileStream, CompressionMode.Decompress))
                            {
                                using (var streamReader = new StreamReader(gzStream, encoding: Encoding.UTF8))
                                {
                                    string json;
                                    while ((json = await streamReader.ReadLineAsync()) != null)
                                    {
                                        var data = JsonConvert.DeserializeObject(json, JsonSerializerSettings);
                                        var evt  = LogEventReader.ReadFromJObject(data as JObject, messageFields);
                                        {
                                            analogy.Write(evt);
                                            AnalogyLogMessage m = CommonParser.ParseLogEventProperties(evt);
                                            parsedMessages.Add(m);
                                        }
                                    }
                                }
                            }
                        }


                        using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
                        {
                            string json;
                            while ((json = await streamReader.ReadLineAsync()) != null)
                            {
                                var data = JsonConvert.DeserializeObject(json);
                                var evt  = LogEventReader.ReadFromJObject(data as JObject, messageFields);
                                {
                                    analogy.Write(evt);
                                    AnalogyLogMessage m = CommonParser.ParseLogEventProperties(evt);
                                    parsedMessages.Add(m);
                                }
                            }
                        }
                    }

                    messagesHandler.AppendMessages(parsedMessages, fileName);
                    return(parsedMessages);
                }
            }
            catch (Exception e)
            {
                AnalogyLogMessage empty = new AnalogyLogMessage($"Error reading file {fileName}: Error: {e.Message}",
                                                                AnalogyLogLevel.Error, AnalogyLogClass.General, "Analogy", "None");
                empty.Source = nameof(CompactJsonFormatParser);
                empty.Module = "Analogy.LogViewer.Serilog";
                parsedMessages.Add(empty);
                messagesHandler.AppendMessages(parsedMessages, fileName);
                return(parsedMessages);
            }
        }
示例#5
0
        public async Task <IEnumerable <AnalogyLogMessage> > Process(string fileName, CancellationToken token, ILogMessageCreatedHandler messagesHandler)
        {
            var messages = await Task <IEnumerable <AnalogyLogMessage> > .Factory.StartNew(() =>
            {
                List <AnalogyLogMessage> parsedMessages = new List <AnalogyLogMessage>();
                try
                {
                    using (var analogy = new LoggerConfiguration()
                                         .WriteTo.Analogy()
                                         .CreateLogger())
                    {
                        using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            if (fileName.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase))
                            {
                                using (var gzStream = new GZipStream(fileStream, CompressionMode.Decompress))
                                {
                                    using (var clef = new StreamReader(gzStream, encoding: Encoding.UTF8))
                                    {
                                        var reader = new LogEventReader(clef);
                                        while (reader.TryRead(out var evt))
                                        {
                                            analogy.Write(evt);
                                            AnalogyLogMessage m = CommonParser.ParseLogEventProperties(evt);
                                            parsedMessages.Add(m);
                                        }

                                        messagesHandler.AppendMessages(parsedMessages, fileName);
                                        return(parsedMessages);
                                    }
                                }
                            }
                            else
                            {
                                using (var clef = new StreamReader(fileStream, encoding: Encoding.UTF8))
                                {
                                    var reader = new LogEventReader(clef);
                                    while (reader.TryRead(out var evt))
                                    {
                                        analogy.Write(evt);
                                        AnalogyLogMessage m = CommonParser.ParseLogEventProperties(evt);
                                        parsedMessages.Add(m);
                                    }

                                    messagesHandler.AppendMessages(parsedMessages, fileName);
                                    return(parsedMessages);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    AnalogyLogMessage empty = new AnalogyLogMessage($"Error reading file {fileName}: Error: {e.Message}",
                                                                    AnalogyLogLevel.Error, AnalogyLogClass.General, "Analogy", "None");
                    empty.Source = nameof(ClefParser);
                    empty.Module = "Analogy.LogViewer.Serilog";
                    parsedMessages.Add(empty);
                    messagesHandler.AppendMessages(parsedMessages, fileName);
                    return(parsedMessages);
                }
            });

            return(messages);
        }