Esempio n. 1
0
        public void ReadData(string path, string componentName, IDbProcessor dbProcessor)
        {
            System.Net.HttpListener web = null;
            try
            {
                web = new System.Net.HttpListener();
                web.Prefixes.Add(path);
                web.Start();

                do
                {
                    var ctx  = web.GetContext();
                    var req  = ctx.Request;
                    var data = new StreamReader(req.InputStream, req.ContentEncoding).ReadToEnd();

                    if (ProcessorMonitorContainer.ComponentStopper[componentName])
                    {
                        ProcessorMonitorContainer.ComponentStopper[componentName] = false;
                        break;
                    }

                    // parse json into an Entry array
                    var entries = JsonConvert.DeserializeObject <IEntry[]>(data);

                    foreach (var entry in entries)
                    {
                        var lvlType = LevelTypesHelper.GetLevelTypeFromString(entry.Level);

                        IEntry newEntry = EntryFactory.CreateNewEntry
                                          (
                            entry.Timestamp,
                            $"{entry.RenderedMessage}",
                            (int)lvlType,
                            componentName,
                            entry.Exception
                                          );
                        dbProcessor.WriteOne(componentName, newEntry);
                    }
                } while (!ProcessorMonitorContainer.ComponentStopper[componentName]);
            }
            finally
            {
                if (web != null && web.IsListening)
                {
                    web.Stop();
                }
            }
        }
Esempio n. 2
0
        public void ReadData(string path, string componentName, IDbProcessor dbProcessor)
        {
            UdpClient listener = null;

            try
            {
                // get the address and port to listen
                var splitPath = path.Split(':');
                var localAddr = IPAddress.Parse(splitPath[0]);
                var port      = int.Parse(splitPath[1]);

                listener = new UdpClient(port);
                var groupEP = new IPEndPoint(localAddr, port);

                do
                {
                    var bytes = listener.Receive(ref groupEP);

                    if (ProcessorMonitorContainer.ComponentStopper[componentName])
                    {
                        ProcessorMonitorContainer.ComponentStopper[componentName] = false;
                        break;
                    }

                    var data    = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
                    var ent     = JsonConvert.DeserializeObject <IEntry>(data);
                    var lvlType = LevelTypesHelper.GetLevelTypeFromString(ent.Level);

                    // save entry
                    IEntry newEntry = EntryFactory.CreateNewEntry
                                      (
                        ent.Timestamp,
                        $"{ent.RenderedMessage} {ent.Message}",
                        (int)lvlType,
                        componentName,
                        ent.Exception
                                      );
                    dbProcessor.WriteOne(componentName, newEntry);
                } while (!ProcessorMonitorContainer.ComponentStopper[componentName]);
            }
            finally
            {
                listener?.Close();
            }
        }
Esempio n. 3
0
        public void ReadData(string path, string componentName, IDbProcessor dbProcessor)
        {
            using (var sr = new StreamReader(path))
            {
                try
                {
                    string line = null;
                    var    sb   = new StringBuilder();

                    while ((line = sr.ReadLine()) != null && !ProcessorMonitorContainer.ComponentStopper[componentName])
                    {
                        if (ProcessorMonitorContainer.ComponentStopper[componentName])
                        {
                            ProcessorMonitorContainer.ComponentStopper[componentName] = false;
                            break;
                        }

                        var levelInit = line.IndexOf('[');
                        var levelEnd  = line.IndexOf(']');
                        var split     = line.Split(' ');

                        var isValid = !(split.Length < 5 || levelInit == -1 || levelEnd == -1 || levelEnd - levelInit != 4);

                        if (!isValid) // add to queue
                        {
                            sb.AppendLine(line);
                        }
                        else
                        {
                            if (sb.Length == 0) // first valid line
                            {
                                sb.AppendLine(line);
                            }
                            else
                            {
                                // previous event lines
                                var prevLines = sb.ToString().TrimEnd();
                                var lvlRaw    = prevLines.Substring(levelInit + 1, 3);
                                var lvlType   = LevelTypesHelper.GetLevelTypeFromString(lvlRaw);

                                // save entry
                                IEntry newEntry = EntryFactory.CreateNewEntry
                                                  (
                                    DateTime.Parse(prevLines.Substring(0, 29)),
                                    prevLines.Substring(levelEnd + 1),
                                    (int)lvlType,
                                    componentName
                                                  );
                                dbProcessor.WriteOne(componentName, newEntry);

                                // remove previous event
                                sb.Clear();

                                // add current event to queue
                                sb.AppendLine(line);
                            }
                        }
                    }
                }
                finally
                {
                    sr?.Close();
                }
            }

            GC.Collect();
        }
Esempio n. 4
0
        public void ReadData(string path, string componentName, IDbProcessor dbProcessor)
        {
            System.Net.Sockets.TcpListener server = null;

            try
            {
                // get the address and port to listen
                var splitPath = path.Split(':');
                var localAddr = IPAddress.Parse(splitPath[0]);
                var port      = int.Parse(splitPath[1]);

                // Set the TcpListener port and address.
                server = new System.Net.Sockets.TcpListener(localAddr, port);

                // Start listening for client requests.
                server.Start();

                // Buffer for reading data
                var           bytes  = new byte[256];
                NetworkStream stream = null;
                TcpClient     client = null;
                int           i;

                // Enter the listening loop.
                do
                {
                    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    client = server.AcceptTcpClient();

                    // Get a stream object for reading and writing
                    stream = client.GetStream();

                    if (ProcessorMonitorContainer.ComponentStopper[componentName])
                    {
                        ProcessorMonitorContainer.ComponentStopper[componentName] = false;
                        break;
                    }

                    // Loop to receive all the data sent by the client.
                    var sb = new StringBuilder();
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        // Translate data bytes to a ASCII string.
                        sb.Append(Encoding.ASCII.GetString(bytes, 0, i));
                    }

                    // open json array
                    // Note: the objects received from Serilog don't arrive with a json array format, so we have to force it
                    sb.Insert(0, '[');

                    // close json array
                    sb.Append(']');

                    // add comma to each json object
                    sb.Replace(@"{""timestamp""", @",{""timestamp""");

                    // replace first comma with empty space
                    sb[1] = ' ';

                    // parse json into an Entry array
                    var ents = JArray.Parse(sb.ToString());

                    foreach (var item in ents)
                    {
                        var ent     = JsonConvert.DeserializeObject <IEntry>(item.ToString());
                        var lvlType = LevelTypesHelper.GetLevelTypeFromString(ent.Level);

                        IEntry newEntry = EntryFactory.CreateNewEntry
                                          (
                            ent.Timestamp,
                            $"{ent.RenderedMessage} {ent.Message}",
                            (int)lvlType,
                            componentName,
                            ent.Exception
                                          );
                        dbProcessor.WriteOne(componentName, newEntry);
                    }

                    // Shutdown and end connection
                    client.Close();
                } while (!ProcessorMonitorContainer.ComponentStopper[componentName]);
            }
            finally
            {
                // Stop listening for new clients.
                server?.Stop();
            }
        }