public static void QueueLogObject(StackifyLib.Models.LogMsg msg) { try { if (PrefixEnabled() || _LogClient.CanQueue()) { if (msg.Ex != null) { if (string.IsNullOrEmpty(msg.Level)) { msg.Level = "ERROR"; } string origMsg = msg.Msg; if (msg.Msg != null && msg.Ex != null) { msg.Msg += "\r\n" + msg.Ex.ToString(); } else if (msg.Msg == null && msg.Ex != null) { msg.Msg = msg.Ex.ToString(); } bool ignore = StackifyError.IgnoreError(msg.Ex); bool shouldSend = _LogClient.ErrorShouldBeSent(msg.Ex); if (!ignore) { if (!string.IsNullOrEmpty(origMsg)) { msg.Ex.SetAdditionalMessage(origMsg); } if (!shouldSend) { msg.Ex = null; msg.Msg += " #errorgoverned"; } } else { msg.Ex = null; } } _LogClient.QueueMessage(msg); } else { StackifyAPILogger.Log("Unable to send log because the queue is full"); } } catch (Exception ex) { StackifyAPILogger.Log(ex.ToString()); } }
public static void QueueLogObject(StackifyLib.Models.LogMsg msg, Exception exceptionObject) { if (exceptionObject != null) { msg.Ex = StackifyError.New(exceptionObject); } QueueLogObject(msg); }
public static void QueueException(StackifyError error) { var msg = new LogMsg() { Level = "ERROR", Msg = error.Message, Ex = error }; QueueLogObject(msg); }
public static void QueueException(string level, string message, Exception exceptionObject, object debugData = null) { var msg = new LogMsg() { Level = level, Msg = message }; if (debugData != null) { //set json data to pass through as debugging data msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(debugData, true); } QueueLogObject(msg, exceptionObject); }
internal LogMsg Translate(LoggingEvent loggingEvent) { if (loggingEvent == null) return null; //don't log our own logging causing a loop if (loggingEvent.RenderedMessage.IndexOf("StackifyLib:", StringComparison.OrdinalIgnoreCase) > -1) return null; var msg = new LogMsg(); if (loggingEvent.Level != null) { msg.Level = loggingEvent.Level.DisplayName; } try { msg.SrcMethod = loggingEvent.LoggerName; if (logMethodNames ?? false) { var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true); if (frames.Any()) { var first = frames.First(); msg.SrcMethod = first.Method; msg.SrcLine = first.LineNum; } } } catch (Exception ex) { StackifyAPILogger.Log("Errror evaluating source method " + ex.ToString()); } var diags = GetDiagnosticContextProperties(); if (diags != null && diags.ContainsKey("transid")) { msg.TransID = diags["transid"].ToString(); diags.Remove("transid"); } StackifyError error = null; object messageObject = null; string errorAdditionalMessage = null; if (loggingEvent.MessageObject != null && loggingEvent.MessageObject is StackifyError) { //Message Object was an exception error = (StackifyError)loggingEvent.MessageObject; messageObject = error.ToString(); errorAdditionalMessage = null; } else if (loggingEvent.MessageObject != null && loggingEvent.MessageObject is Exception) { //Message Object was an exception error = StackifyError.New((Exception)loggingEvent.MessageObject); messageObject = error.ToString(); errorAdditionalMessage = null; } else if (loggingEvent.ExceptionObject != null) { //Exception was passed in if (loggingEvent.ExceptionObject is StackifyError) { error = loggingEvent.ExceptionObject as StackifyError; } else { error = StackifyError.New(loggingEvent.ExceptionObject); } errorAdditionalMessage = loggingEvent.RenderedMessage; messageObject = loggingEvent.MessageObject; } else { messageObject = loggingEvent.MessageObject; } //messageObject is not an object we need to serialize. if (messageObject == null || messageObject is string || messageObject.GetType().FullName == "log4net.Util.SystemStringFormat") { //passing null to the serialize object since we can't serialize the logged object. We only need to get potential diags. msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags); msg.Msg = loggingEvent.RenderedMessage; } else if (messageObject is StackifyLib.Models.LogMessage) { var item = messageObject as StackifyLib.Models.LogMessage; msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(item.json, true, diags); msg.Msg = item.message; } else { //try to serialize the messageObject since we know its not a string msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(messageObject, false, diags); msg.Msg = loggingEvent.RenderedMessage; if (error != null) { msg.Msg += "\r\n" + error.ToString(); } } if (!string.IsNullOrWhiteSpace(errorAdditionalMessage) && error != null) { //if something besides just the exception was logged, add that message to our error object error.SetAdditionalMessage(errorAdditionalMessage); } else if (msg.Msg == null && error != null) { msg.Msg = error.ToString(); } if (error == null && (loggingEvent.Level == Level.Error || loggingEvent.Level == Level.Fatal)) { StringException stringEx = new StringException(loggingEvent.RenderedMessage); stringEx.TraceFrames = new List<TraceFrame>(); stringEx.TraceFrames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName); if (stringEx.TraceFrames.Any()) { var first = stringEx.TraceFrames.First(); msg.SrcMethod = first.Method; msg.SrcLine = first.LineNum; } //Make error out of log message error = StackifyError.New(stringEx); } if (error != null && !StackifyError.IgnoreError(error) && _logClient.ErrorShouldBeSent(error)) { msg.Ex = error; } else if (error != null && msg.Msg != null) { msg.Msg += " #errorgoverned"; } return msg; }
internal LogMsg Translate(LogEventInfo loggingEvent) { if (loggingEvent == null) return null; //do not log our own messages. This is to prevent any sort of recursion that could happen since calling to send this will cause even more logging to happen if (loggingEvent.FormattedMessage != null && loggingEvent.FormattedMessage.IndexOf("StackifyLib:", StringComparison.OrdinalIgnoreCase) > -1) return null; StackifyLib.Models.LogMsg msg = new LogMsg(); if (loggingEvent.Level != null) { msg.Level = loggingEvent.Level.Name; } if (loggingEvent.HasStackTrace && loggingEvent.UserStackFrame != null) { var frame = loggingEvent.UserStackFrame; MethodBase method = frame.GetMethod(); if (method != (MethodBase) null && method.DeclaringType != (Type) null) { if (method.DeclaringType != (Type) null) { msg.SrcMethod = method.DeclaringType.FullName + "." + method.Name; msg.SrcLine = frame.GetFileLineNumber(); } } } else { msg.SrcMethod = loggingEvent.LoggerName; if (logMethodNames) { var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName); if (frames.Any()) { var first = frames.First(); msg.SrcMethod = first.Method; msg.SrcLine = first.LineNum; } } } StackifyError error = null; if (loggingEvent.Exception != null && loggingEvent.Exception is StackifyError) { error = (StackifyError) loggingEvent.Exception; } else if (loggingEvent.Exception != null) { error = StackifyError.New((Exception)loggingEvent.Exception); } var diags = GetDiagnosticContextProperties(); if (diags != null && diags.ContainsKey("transid")) { msg.TransID = diags["transid"].ToString(); diags.Remove("transid"); } if ((loggingEvent.Parameters != null) && (loggingEvent.Parameters.Length > 0)) { msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(loggingEvent.Parameters[0], true, diags); } else { msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags); } string formattedMessage; //Use the layout render to allow custom fields to be logged, but not if it is the default format as it logs a bunch fields we already log //really no reason to use a layout at all if (this.Layout != null && this.Layout.ToString() != "'${longdate}|${level:uppercase=true}|${logger}|${message}'") //do not use if it is the default { formattedMessage = this.Layout.Render(loggingEvent); } else { formattedMessage = loggingEvent.FormattedMessage; } msg.Msg = formattedMessage; if (msg.Msg != null && error != null) { msg.Msg += "\r\n" + error.ToString(); } else if (msg.Msg == null && error != null) { msg.Msg = error.ToString(); } if (error == null && (loggingEvent.Level == LogLevel.Error || loggingEvent.Level == LogLevel.Fatal)) { StringException stringException = new StringException(msg.Msg); stringException.TraceFrames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName); if (!loggingEvent.HasStackTrace || loggingEvent.UserStackFrame == null) { if (stringException.TraceFrames.Any()) { var first = stringException.TraceFrames.First(); msg.SrcMethod = first.Method; msg.SrcLine = first.LineNum; } } //Make error out of log message error = StackifyError.New(stringException); } if (error != null && !StackifyError.IgnoreError(error) && _logClient.ErrorShouldBeSent(error)) { error.SetAdditionalMessage(formattedMessage); msg.Ex = error; } else if (error != null && msg.Msg != null) { msg.Msg += " #errorgoverned"; } return msg; }
// internal Task<HttpClient.StackifyWebResponse> SendLogs(LogMsg[] messages) // { // try // { // StackifyAPILogger.Log("Trying to SendLogs"); // EnsureHttpClient(); // LogMsgGroup group = new LogMsgGroup(); // var identified = _HttpClient.IdentifyApp(); // if (_HttpClient.IsRecentError()) // { // var tcs = new TaskCompletionSource<HttpClient.StackifyWebResponse>(); // tcs.SetResult(new HttpClient.StackifyWebResponse() { Exception = new ApplicationException("Unable to send logs at this time due to recent error: " + (_HttpClient.LastErrorMessage ?? "")) }); // return tcs.Task; // } // if (!identified) // { // var tcs = new TaskCompletionSource<HttpClient.StackifyWebResponse>(); // tcs.SetResult(new HttpClient.StackifyWebResponse() { Exception = new ApplicationException("Unable to send logs at this time. Unable to identify app") }); // return tcs.Task; // } // group.Msgs = messages.ToList(); // //set these fields even if some could be null // if (_HttpClient.AppIdentity != null) // { // group.CDAppID = _HttpClient.AppIdentity.DeviceAppID; // group.CDID = _HttpClient.AppIdentity.DeviceID; // group.EnvID = _HttpClient.AppIdentity.EnvID; // group.Env = _HttpClient.AppIdentity.Env; // group.AppNameID = _HttpClient.AppIdentity.AppNameID; // group.AppEnvID = _HttpClient.AppIdentity.AppEnvID; // if (!String.IsNullOrWhiteSpace(_HttpClient.AppIdentity.DeviceAlias)) // { // group.ServerName = _HttpClient.AppIdentity.DeviceAlias; // } // if (!String.IsNullOrWhiteSpace(_HttpClient.AppIdentity.AppName)) // { // group.AppName = _HttpClient.AppIdentity.AppName; // } // } // var env = EnvironmentDetail.Get(false); // //We use whatever the identity stuff says, otherwise we use the azure instance name and fall back to the machine name // if (string.IsNullOrEmpty(group.ServerName)) // { // if (!string.IsNullOrEmpty(env.AzureInstanceName)) // { // group.ServerName = env.AzureInstanceName; // } // else // { // group.ServerName = Environment.MachineName; // } // } // //if it wasn't set by the identity call above // if (string.IsNullOrWhiteSpace(group.AppName)) // { // group.AppName = env.AppNameToUse(); // } // else if (group.AppName.StartsWith("/LM/W3SVC")) // { // group.AppName = env.AppNameToUse(); // } // group.AppLoc = env.AppLocation; // if (string.IsNullOrEmpty(group.Env)) // { // group.Env = env.ConfiguredEnvironmentName; // } // group.Logger = _LoggerName; // group.Platform = ".net"; // //string jsonData = SimpleJson.SimpleJson.SerializeObject(group); // string jsonData = JsonConvert.SerializeObject(group, new JsonSerializerSettings() {NullValueHandling = NullValueHandling.Ignore}); // string urlToUse = null; // urlToUse = System.Web.VirtualPathUtility.AppendTrailingSlash(_HttpClient.BaseAPIUrl) + "Log/Save"; // if (!_ServicePointSet) // { // ServicePointManager.FindServicePoint(urlToUse, null).ConnectionLimit = 10; // _ServicePointSet = true; // } // StackifyAPILogger.Log("Sending " + messages.Length.ToString() + " log messages"); // var task = // _HttpClient.SendJsonAndGetResponseAsync( // urlToUse, // jsonData, jsonData.Length > 5000); // return task; // } // catch (Exception ex) // { // Utils.StackifyAPILogger.Log(ex.ToString()); // var tcs = new TaskCompletionSource<HttpClient.StackifyWebResponse>(); // tcs.SetResult(new HttpClient.StackifyWebResponse() { Exception = ex }); //// tcs.SetException(ex); // return tcs.Task; // } // return null; // } internal Task<HttpClient.StackifyWebResponse> SendLogsByGroups(LogMsg[] messages) { try { StackifyAPILogger.Log("Trying to SendLogs"); EnsureHttpClient(); var identified = _HttpClient.IdentifyApp(); if (_HttpClient.IsRecentError()) { var tcs = new TaskCompletionSource<HttpClient.StackifyWebResponse>(); tcs.SetResult(new HttpClient.StackifyWebResponse() { Exception = new ApplicationException("Unable to send logs at this time due to recent error: " + (_HttpClient.LastErrorMessage ?? "")) }); return tcs.Task; } if (!identified) { var tcs = new TaskCompletionSource<HttpClient.StackifyWebResponse>(); tcs.SetResult(new HttpClient.StackifyWebResponse() { Exception = new ApplicationException("Unable to send logs at this time. Unable to identify app") }); return tcs.Task; } var groups = SplitLogsToGroups(messages); string jsonData = JsonConvert.SerializeObject(groups, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); string urlToUse = System.Web.VirtualPathUtility.AppendTrailingSlash(_HttpClient.BaseAPIUrl) + "Log/SaveMultipleGroups"; if (!_ServicePointSet) { ServicePointManager.FindServicePoint(urlToUse, null).ConnectionLimit = 10; _ServicePointSet = true; } StackifyAPILogger.Log("Sending " + messages.Length.ToString() + " log messages via send multi groups"); var task = _HttpClient.SendJsonAndGetResponseAsync( urlToUse, jsonData, jsonData.Length > 5000); messages = null; groups = null; return task; } catch (Exception ex) { Utils.StackifyAPILogger.Log(ex.ToString()); var tcs = new TaskCompletionSource<HttpClient.StackifyWebResponse>(); tcs.SetResult(new HttpClient.StackifyWebResponse() { Exception = ex }); return tcs.Task; } return null; }
private List<Models.LogMsgGroup> SplitLogsToGroups(LogMsg[] messages) { Dictionary<string, Models.LogMsgGroup> groups = new Dictionary<string, LogMsgGroup>(); foreach (var message in messages) { string groupKey = "default"; if (message.AppDetails != null) { groupKey = message.AppDetails.GetUniqueKey(); } if (!groups.ContainsKey(groupKey)) { if (groupKey == "default" || message.AppDetails == null) { groups["default"] = CreateDefaultMsgGroup(); } else { var defaults = CreateDefaultMsgGroup(); //default app, env, and server name if not set to whatever the current one is //do not default the other fields as they not match what is being set. //i.e. the default appnameid is not the correct id for a new custom app name being used. var d = message.AppDetails; var group = new LogMsgGroup() { AppEnvID = d.AppEnvID, AppLoc = d.AppLoc, AppName = d.AppName ?? defaults.AppName, AppNameID = d.AppNameID, CDAppID = d.CDAppID, CDID = d.CDID, Env = d.Env ?? defaults.Env, EnvID = d.EnvID, Logger = d.Logger ?? _LoggerName, Platform = ".net", ServerName = d.ServerName ?? defaults.ServerName, Msgs = new List<LogMsg>() }; groups[groupKey] = group; } } groups[groupKey].Msgs.Add(message); } return groups.Values.ToList(); }
public void QueueMessage(LogMsg msg) { if (msg == null) return; int isError = 0; if (msg.id == null) //should be null unless someone is using our API directly and setting it { msg.id = SequentialGuid.NewGuid().ToString(); } if (msg.Ex != null) { isError = 1; } //Used by Stackify profiler only msg.SetLogMsgID(msg.id, isError, msg.Level, msg.Msg, msg.data); //We need to do everything up to this point for sasquatch. Even if we aren't uploading the log. if (this.CanQueue()) { _LogQueue.QueueLogMessage(msg); } }
private List<Models.LogMsgGroup> SplitLogsToGroups(LogMsg[] messages) { Dictionary<string, Models.LogMsgGroup> groups = new Dictionary<string, LogMsgGroup>(); foreach (var message in messages) { string groupKey = "default"; if (message.AppDetails != null) { groupKey = message.AppDetails.GetUniqueKey(); } if (!groups.ContainsKey(groupKey)) { if (groupKey == "default" || message.AppDetails == null) { groups["default"] = CreateDefaultMsgGroup(); } else { var d = message.AppDetails; var group = new LogMsgGroup() { AppEnvID = d.AppEnvID, AppLoc = d.AppLoc, AppName = d.AppName, AppNameID = d.AppNameID, CDAppID = d.CDAppID, CDID = d.CDID, Env = d.Env, EnvID = d.EnvID, Logger = _LoggerName, Platform = ".net", ServerName = d.ServerName, Msgs = new List<LogMsg>() }; groups[groupKey] = group; } } groups[groupKey].Msgs.Add(message); } return groups.Values.ToList(); }
internal Task<HttpClient.StackifyWebResponse> SendLogs(LogMsg[] messages) { try { StackifyAPILogger.Log("Trying to SendLogs"); EnsureHttpClient(); LogMsgGroup group = new LogMsgGroup(); var identified = _HttpClient.IdentifyApp(); if (_HttpClient.IsRecentError()) { var tcs = new TaskCompletionSource<HttpClient.StackifyWebResponse>(); tcs.SetResult(new HttpClient.StackifyWebResponse() { Exception = new ApplicationException("Unable to send logs at this time due to recent error: " + (_HttpClient.LastErrorMessage ?? "")) }); return tcs.Task; } if (!identified) { var tcs = new TaskCompletionSource<HttpClient.StackifyWebResponse>(); tcs.SetResult(new HttpClient.StackifyWebResponse() { Exception = new ApplicationException("Unable to send logs at this time. Unable to identify app") }); return tcs.Task; } group.Msgs = messages.ToList(); //set these fields even if some could be null if (_HttpClient.AppIdentity != null) { group.CDAppID = _HttpClient.AppIdentity.DeviceAppID; group.CDID = _HttpClient.AppIdentity.DeviceID; group.EnvID = _HttpClient.AppIdentity.EnvID; group.Env = _HttpClient.AppIdentity.Env; group.AppNameID = _HttpClient.AppIdentity.AppNameID; group.AppEnvID = _HttpClient.AppIdentity.AppEnvID; if (!String.IsNullOrWhiteSpace(_HttpClient.AppIdentity.DeviceAlias)) { group.ServerName = _HttpClient.AppIdentity.DeviceAlias; } if (!String.IsNullOrWhiteSpace(_HttpClient.AppIdentity.AppName)) { group.AppName = _HttpClient.AppIdentity.AppName; } } var env = EnvironmentDetail.Get(false); //We use whatever the identity stuff says, otherwise we use the azure instance name and fall back to the machine name if (string.IsNullOrEmpty(group.ServerName)) { if (!string.IsNullOrEmpty(env.AzureInstanceName)) { group.ServerName = env.AzureInstanceName; } else { group.ServerName = Environment.MachineName; } } //if it wasn't set by the identity call above if (string.IsNullOrWhiteSpace(group.AppName)) { group.AppName = env.AppNameToUse(); } else if (group.AppName.StartsWith("/LM/W3SVC")) { group.AppName = env.AppNameToUse(); } group.AppLoc = env.AppLocation; if (string.IsNullOrEmpty(group.Env)) { group.Env = env.ConfiguredEnvironmentName; } group.Logger = _LoggerName; group.Platform = ".net"; //string jsonData = SimpleJson.SimpleJson.SerializeObject(group); string jsonData = JsonConvert.SerializeObject(group, new JsonSerializerSettings() {NullValueHandling = NullValueHandling.Ignore}); string urlToUse = null; urlToUse = System.Web.VirtualPathUtility.AppendTrailingSlash(_HttpClient.BaseAPIUrl) + "Log/Save"; if (!_ServicePointSet) { ServicePointManager.FindServicePoint(urlToUse, null).ConnectionLimit = 10; _ServicePointSet = true; } StackifyAPILogger.Log("Sending " + messages.Length.ToString() + " log messages"); var task = _HttpClient.SendAndGetResponseAsync( urlToUse, jsonData, jsonData.Length > 5000); return task; } catch (Exception ex) { Utils.StackifyAPILogger.Log(ex.ToString()); var tcs = new TaskCompletionSource<HttpClient.StackifyWebResponse>(); tcs.SetResult(new HttpClient.StackifyWebResponse() { Exception = ex }); // tcs.SetException(ex); return tcs.Task; } return null; }
public void QueueMessage(LogMsg msg) { if (msg.id == null) { int isError = 0; if (msg.Ex != null) { isError = 1; } msg.SetLogMsgID(SequentialGuid.NewGuid().ToString(), isError, msg.Level); } _LogQueue.QueueLogMessage(msg); }
public void QueueMessage(LogMsg msg) { var action = OnQueueMessage; if (action != null) { action.Invoke(msg); } }