Esempio n. 1
0
        /// <summary>
        /// Writes an entry to the log
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public bool WriteEntry(WebLogEntry entry)
        {
            lock (_writeLock)
            {
                if (entry.Id == 0)
                    entry.Id = entry.GetHashCode();

                string logFilename = ConnectionString;
                bool writeEndDoc = true;
                FileStream fileStream = null;
                try
                {
                    fileStream = new FileStream(logFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
                    fileStream.Seek(0, SeekOrigin.End);

                    // *** If the file's not empty start writing over the end doc tag
                    // *** We'll rewrite it at the end
                    if (fileStream.Position > 0)
                        fileStream.Seek(-1 * "</ApplicationLog>\r\n".Length, SeekOrigin.End);
                }
                catch
                {
                    return false;
                }

                XmlTextWriter writer = new XmlTextWriter((Stream)fileStream, Encoding.UTF8);
                writer.Formatting = Formatting.Indented;
                writer.IndentChar = ' ';
                writer.Indentation = 4;

                // *** If the file is empty write the root element
                if (fileStream.Position == 0)
                {
                    writer.WriteStartElement("ApplicationLog");
                    writeEndDoc = false; // it'll automatically unwind the StartElement
                }

                writer.WriteStartElement("LogEntry");
                writer.WriteElementString("Id", entry.Id.ToString());

                writer.WriteStartElement("Entered");
                writer.WriteValue(entry.Entered);
                writer.WriteEndElement();

                writer.WriteElementString("Message", entry.Message);
                writer.WriteElementString("ErrorLevel", entry.ErrorLevel.ToString());
                writer.WriteElementString("Details", entry.Details);

                writer.WriteElementString("Url", entry.Url);
                writer.WriteElementString("QueryString", entry.QueryString);
                writer.WriteElementString("UserAgent", entry.UserAgent);
                writer.WriteElementString("Referrer", entry.Referrer);
                writer.WriteElementString("PostData", entry.PostData);
                writer.WriteElementString("IpAddress", entry.IpAddress);
                writer.WriteElementString("RequestDuration", entry.RequestDuration.ToString());

                writer.WriteEndElement(); // error

                if (writeEndDoc)
                    writer.WriteRaw("\r\n</ApplicationLog>\r\n");
                else
                {
                    writer.WriteEndElement();
                    writer.WriteRaw("\r\n");
                }

                writer.Close();
                fileStream.Close();

                return true;
            }
        }
        /// <summary>
        /// Writes an entry to the log
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public bool WriteEntry(WebLogEntry entry)
        {
            lock (_writeLock)
            {
                if (entry.Id == 0)
                {
                    entry.Id = entry.GetHashCode();
                }


                string     logFilename = ConnectionString;
                bool       writeEndDoc = true;
                FileStream fileStream  = null;
                try
                {
                    fileStream = new FileStream(logFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
                    fileStream.Seek(0, SeekOrigin.End);

                    // *** If the file's not empty start writing over the end doc tag
                    // *** We'll rewrite it at the end
                    if (fileStream.Position > 15)
                    {
                        try
                        {
                            fileStream.Seek(-1 * "</ApplicationLog>\r\n".Length, SeekOrigin.End);
                        }catch { }
                    }
                }
                catch
                {
                    return(false);
                }

                XmlTextWriter writer = new XmlTextWriter((Stream)fileStream, Encoding.UTF8);
                writer.Formatting  = Formatting.Indented;
                writer.IndentChar  = ' ';
                writer.Indentation = 4;

                // *** If the file is empty write the root element
                if (fileStream.Position == 0)
                {
                    writer.WriteStartElement("ApplicationLog");
                    writeEndDoc = false; // it'll automatically unwind the StartElement
                }

                writer.WriteStartElement("LogEntry");
                writer.WriteElementString("Id", entry.Id.ToString());

                writer.WriteStartElement("Entered");
                writer.WriteValue(entry.Entered);
                writer.WriteEndElement();

                writer.WriteElementString("Message", entry.Message);
                writer.WriteElementString("ErrorLevel", entry.ErrorLevel.ToString());
                writer.WriteElementString("Details", entry.Details);

                writer.WriteElementString("Url", entry.Url);
                writer.WriteElementString("QueryString", entry.QueryString);
                writer.WriteElementString("UserAgent", entry.UserAgent);
                writer.WriteElementString("Referrer", entry.Referrer);
                writer.WriteElementString("PostData", entry.PostData);
                writer.WriteElementString("IpAddress", entry.IpAddress);
                writer.WriteElementString("RequestDuration", entry.RequestDuration.ToString());

                writer.WriteEndElement(); // error


                if (writeEndDoc)
                {
                    writer.WriteRaw("\r\n</ApplicationLog>\r\n");
                }
                else
                {
                    writer.WriteEndElement();
                    writer.WriteRaw("\r\n");
                }

                writer.Close();
                fileStream.Close();

                return(true);
            }
        }
        /// <summary>
        /// Writes an entry to the log
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public bool WriteEntry(WebLogEntry entry)
        {
            if (entry.Id == 0)
            {
                entry.Id = entry.GetHashCode();
            }


            string logFilename = ConnectionString;
            bool   writeEndDoc = true;

            lock (_writeLock)
            {
                try
                {
                    using (var fileStream = new FileStream(logFilename, FileMode.OpenOrCreate, FileAccess.Write))
                        using (var sw = new StreamWriter(fileStream))
                        {
                            fileStream.Seek(0, SeekOrigin.End);

                            sw.WriteLine($"Time:       {entry.Entered.ToString("yyyy-MM-dd HH:mm:ss")}");
                            sw.WriteLine($"Id:         {entry.Id}");
                            sw.WriteLine($"Message:    {entry.Message}");
                            sw.WriteLine($"ErrorLevel: {entry.ErrorLevel}");


                            if (!string.IsNullOrEmpty(entry.Url))
                            {
                                sw.WriteLine($"Url:        {entry.Url}");
                            }
                            if (!string.IsNullOrEmpty(entry.QueryString))
                            {
                                sw.WriteLine($"Query:      {entry.QueryString}");
                            }
                            if (!string.IsNullOrEmpty(entry.Referrer))
                            {
                                sw.WriteLine($"Referrer:   {entry.Referrer}");
                            }
                            if (!string.IsNullOrEmpty(entry.IpAddress))
                            {
                                sw.WriteLine($"IpAddress:  {entry.IpAddress}");
                            }
                            if (!string.IsNullOrEmpty(entry.UserAgent))
                            {
                                sw.WriteLine($"UserAgent:  {entry.UserAgent}");
                            }

                            if (!string.IsNullOrEmpty(entry.PostData))
                            {
                                sw.WriteLine($"PostData:   {entry.PostData.Replace("&","\r\n")}");
                            }

                            if (!string.IsNullOrEmpty(entry.Details))
                            {
                                sw.WriteLine($"Details:    {entry.Details}");
                            }

                            if (entry.RequestDuration > 0)
                            {
                                sw.WriteLine($"Duration:       {entry.RequestDuration}");
                            }


                            sw.WriteLine("----------------------------------------");
                        }
                }
                catch
                {
                    return(false);
                }
            }

            return(true);
        }