using BuddyFinance.Integration.Models.Enums; using BuddyFinance.Model; using System; using System.Data.Entity.Validation; using System.Text; using System.Threading.Tasks; /// /// Created by Michelle /// namespace BuddyFinance.Integration.Logging { public class Logger { public static void Log(LogTypeEnum type, string logDescription, int? userId, string ipaddress, string userAgent) { // perform log try { using (var db = new BuddyFinanceDBEntities()) { var log = new ActivityLog { Description = logDescription, TimeStamp = DateTime.Now, IpAddress = ipaddress, Type = type.ToString(), UserId = userId, UserAgent = userAgent }; db.ActivityLogs.Add(log); db.SaveChanges(); } } catch (Exception) { //Ignored } } public static void LogError(Exception ex, int? userId, string ipaddress, string userAgent) { var exception = new StringBuilder(); if (ex is DbEntityValidationException) { foreach (var entityValidationError in ((DbEntityValidationException)ex).EntityValidationErrors) { exception.AppendFormat("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:\n", entityValidationError.Entry.Entity.GetType().Name, entityValidationError.Entry.State); foreach (var ve in entityValidationError.ValidationErrors) { exception.AppendFormat("- Property: \"{0}\", Error: \"{1}\"\n", ve.PropertyName, ve.ErrorMessage); } exception.AppendFormat(ex.ToString()); } } else { exception.Append(ex.ToString()); } Task.Factory.StartNew(() => { Log(LogTypeEnum.Error, exception.ToString(), userId, ipaddress, userAgent); }); } //public static void LogActivity(LogTypeEnum type ,string description,int userid, string ipaddress, //string useragent) //{ // Task.Factory.StartNew(() => // { // Log(type, description, userid, ipaddress, useragent); // }); //} //public static void LogMailNotification(int? initiativeid, string type, string to, string from, string subject, string body, string status, // string moreinfo, bool isRetry, bool isreply) //{ // try // { // using ( // var conn = // new SqlConnection( // ConfigurationManager.ConnectionStrings["LinkageDB"].ConnectionString)) // using (var command = new SqlCommand("LogMailNotification", conn) // { // CommandType = CommandType.StoredProcedure // }) // { // conn.Open(); // command.Parameters.AddWithValue("@initiativeid", initiativeid); // command.Parameters.AddWithValue("@type", type); // command.Parameters.AddWithValue("@to", to); // command.Parameters.AddWithValue("@from", from); // command.Parameters.AddWithValue("@subject", subject); // command.Parameters.AddWithValue("@body", body); // command.Parameters.AddWithValue("@status", status); // command.Parameters.AddWithValue("@moreinfo", moreinfo ?? ""); // command.Parameters.AddWithValue("@isRetry", isRetry); // command.Parameters.AddWithValue("@isreply", isreply); // command.ExecuteNonQuery(); // } // } // catch (Exception ex) // { // //Log(LogTypeEnum type, string logDescription, int userId, string ipaddress, string userAgent) // Log(LogTypeEnum.Error, "Error while logging email notification. => " + ex.ToString(), 0, "Logging", "Logging"); // } //} } }