public override void Log(ExceptionLoggerContext context)
        {
            var dict = new Dictionary <string, object>();

            string userId, userName;
            var    user = context.RequestContext.Principal as ClaimsPrincipal;

            Helpers.GetUserData(dict, user, out userId, out userName);

            string location;

            Helpers.GetLocationForApiCall(context.RequestContext, dict, out location);

            var errorId = Guid.NewGuid().ToString();

            // This is here because the Logger is called BEFORE the Handler in the
            //Web API exception pipeline
            context.Exception.Data.Add("ErrorId", errorId);

            var logEntry = new LogDetail()
            {
                CorrelationId  = errorId,
                Product        = _productName,
                Layer          = "API",
                Location       = location,
                Hostname       = Environment.MachineName,
                Exception      = context.Exception,
                UserId         = userId,
                UserName       = userName,
                AdditionalInfo = dict
            };

            McsLogger.WriteError(logEntry);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Serilog.Debugging.SelfLog.Enable(Console.Error);

            if (args is null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            var fd = GetLogDetail("starting application", null);

            McsLogger.WriteDiagnostic(fd);
            new PerfTracker("FloggerConsole_Execution", "", fd.UserName,
                            fd.Location, fd.Product, fd.Layer);

            try
            {
                var ex = new Exception("Something bad has happened!");
                ex.Data.Add("input param", "nothing to see here");
                throw ex;
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                fd = GetLogDetail("", ex);
                McsLogger.WriteError(fd);
            }
#pragma warning restore CA1031 // Do not catch general exception types

            var connStr = ConfigurationManager.ConnectionStrings["LogConnection"].ConnectionString;
            using (var db = new SqlConnection(connStr))
            {
                db.Open();
                try
                {
                    var rawAdoSp = new SqlCommand("CreateNewCustomer", db)
                    {
                        CommandType = System.Data.CommandType.StoredProcedure
                    };
                    rawAdoSp.Parameters.Add(new SqlParameter("@Name", "waytoolongforitsowngood"));
                    rawAdoSp.Parameters.Add(new SqlParameter("@TotalPurchases", 12000));
                    rawAdoSp.Parameters.Add(new SqlParameter("@TotalReturns", 100.50M));
                    rawAdoSp.ExecuteNonQuery();
                    rawAdoSp.Dispose();
                    var sp = new MCS.Logging.DotNetFramework.Data.CustomAdo.StoredProcedure(db, "CreateNewCustomer");
                    sp.SetParam("@Name", "waytoolongforitsowngood");
                    sp.SetParam("@TotalPurchases", 12000);
                    sp.SetParam("@TotalReturns", 100.50M);
                    sp.ExecNonQuery();
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
                {
                    var efd = GetLogDetail("", ex);
                    McsLogger.WriteError(efd);
                }
#pragma warning restore CA1031 // Do not catch general exception types
            }
            Console.ReadKey();
        }
        public static void LogWebDiagnostic(string product, string layer, string message,
                                            Dictionary <string, object> diagnosticInfo = null)
        {
            var writeDiagnostics = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableDiagnostics"]);

            if (!writeDiagnostics)  // doing this to avoid going through all the data - user, session, etc.
            {
                return;
            }

            var webInfo = GetWebLoggingData(out string userId, out string userName, out string location);

            if (diagnosticInfo != null)
            {
                foreach (var key in diagnosticInfo.Keys)
                {
                    webInfo.Add(key, diagnosticInfo[key]);
                }
            }

            var diagInfo = new LogDetail()
            {
                Product        = product,
                Layer          = layer,
                Location       = location,
                UserId         = userId,
                UserName       = userName,
                Hostname       = Environment.MachineName,
                CorrelationId  = HttpContext.Current.Session.SessionID,
                Message        = message,
                AdditionalInfo = webInfo
            };

            McsLogger.WriteDiagnostic(diagInfo);
        }
        public static void LogWebUsage(string product, string layer, string activityName,
                                       Dictionary <string, object> additionalInfo = null)
        {
            var webInfo = GetWebLoggingData(out string userId, out string userName, out string location);

            if (additionalInfo != null)
            {
                foreach (var key in additionalInfo.Keys)
                {
                    webInfo.Add($"Info-{key}", additionalInfo[key]);
                }
            }

            var usageInfo = new LogDetail()
            {
                Product        = product,
                Layer          = layer,
                Location       = location,
                UserId         = userId,
                UserName       = userName,
                Hostname       = Environment.MachineName,
                CorrelationId  = HttpContext.Current.Session.SessionID,
                Message        = activityName,
                AdditionalInfo = webInfo
            };

            McsLogger.WriteUsage(usageInfo);
        }
        public static void LogWebError(string product, string layer, Exception ex)
        {
            var webInfo = GetWebLoggingData(out string userId, out string userName, out string location);

            var errorInformation = new LogDetail()
            {
                Product        = product,
                Layer          = layer,
                Location       = location,
                UserId         = userId,
                UserName       = userName,
                Hostname       = Environment.MachineName,
                CorrelationId  = HttpContext.Current.Session?.SessionID,
                Exception      = ex,
                AdditionalInfo = webInfo
            };

            McsLogger.WriteError(errorInformation);
        }