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)
        {
            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", exception.Message);
                cmd.Parameters.AddWithValue("@userName", userName);
                cmd.Parameters.AddWithValue("@dateTime", dateTime);
                cmd.Parameters.AddWithValue("@errorDetails", exception.ToString());
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                EmailSender.SendNotification(ex, exception, userName, dateTime);
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
                if (conn != null && conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
        }
    }
}