//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;

namespace CommunityServer.Components
{
    public class EventLogs
    {
        private EventLogs(){}

        public static void Write(EventLogEntry entry)
        {
            try
            {
                if(Globals.IsNullorEmpty(entry.MachineName))
                    entry.MachineName = Environment.MachineName;

                if(!Globals.IsNullorEmpty(entry.Message))
                    entry.Message = Globals.HtmlEncode(entry.Message);

                CommonDataProvider.Instance().WriteEventLogEntry(entry);
            }
            catch{}//fail quietly
        }

        public static void StackTrack()
        {
            Write((new System.Diagnostics.StackTrace()).ToString(),"Stack Track", 999,EventType.Information);
        }

        public static void StackTrack(string category)
        {
            Write((new System.Diagnostics.StackTrace()).ToString(),category, 999,EventType.Information);
        }


        public static void Warn(string message, string category, int eventID, int settingsID)
        {
            Write(message,category,eventID,EventType.Warning,settingsID);
        }

        public static void Warn(string message, string category, int eventID, Exception ex, int settingsID)
        {
            Write(string.Format("{0}\n\n{1}\n{2}", message,ex.GetType(),ex.Message) ,category,eventID,EventType.Warning,settingsID);
        }

        public static void Warn(string message, string category, int eventID)
        {
            Warn(message,category,eventID,-1);
        }

        public static void Info(string message, string category, int eventID, int settingsID)
        {
            Write(message,category,eventID,EventType.Information,settingsID);
        }

        public static void Info(string message, string category, int eventID)
        {
           Info(message,category,eventID,-1);
        }     

        [System.Diagnostics.Conditional("DEBUG")]
        public static void Debug(string message, string category, int eventID, int settingsID)
        {
             Write(message,category,eventID,EventType.Debug,settingsID);
        }

        [System.Diagnostics.Conditional("DEBUG")]
        public static void Debug(string message, string category, int eventID)
        {
            Debug(message,category,eventID,-1);
        }

        public static void Write(string message, string category, int eventID, EventType eventType)
        {
            Write(message,category,eventID,eventType,-1);
        }

        public static void Write(string message, string category, int eventID, EventType eventType, int settingsID)
        {
            EventLogEntry entry = new EventLogEntry();
            entry.Message = message;
            entry.Category = category;
            entry.EventID = eventID;
            entry.EventType = eventType;
            entry.SettingsID = settingsID;
            
            Write(entry);
        }

        public static void Clear(DateTime date)
        {
            CommonDataProvider.Instance().ClearEventLog(date);
        }

    }
}