using System;
using System.Data;
using System.Data.SqlClient;

namespace Baza_Reklam.Classes
{
    public class Logger
    {
        public static void LogException(Exception exception, string userName, DateTime dateTime)
        {
            LogException(exception.Message, exception.ToString(), userName, dateTime);
        }

        public static void LogException(string errorMessage, string errorDetails, string userName, DateTime dateTime)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            try
            {
                conn = new SqlConnection(ConnString.getConnString().Value);
                conn.Open();
                cmd = new SqlCommand("dbo.sp_LogException", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@errorMessage", errorMessage);
                cmd.Parameters.AddWithValue("@userName", userName);
                cmd.Parameters.AddWithValue("@dateTime", dateTime);
                cmd.Parameters.AddWithValue("@errorDetails", errorDetails);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                EmailSender.SendExceptonNotification(ex, errorMessage, errorDetails, userName, dateTime);
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
                if (conn != null && conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
        }
    }
}