public static void Log(System.Object message, int StackTraceFrameOffset = 0) { var stackTraceFrame = new System.Diagnostics.StackTrace(true).GetFrame(1 + StackTraceFrameOffset); var line = stackTraceFrame.GetFileLineNumber(); var method = stackTraceFrame.GetMethod(); if (message.GetType().IsArray) { string arrayMessage = "array["; foreach (System.Object obj in message as System.Object[]) { if (obj == null) { arrayMessage += "\n-<i>null</i>"; } else { arrayMessage += "\n-" + obj.ToString(); } } arrayMessage += "\n]"; UnityEngine.Debug.Log(string.Format(LOG_TEXT, method, line, arrayMessage)); } else { UnityEngine.Debug.Log(string.Format(LOG_TEXT, method, line, message)); } }
/// <summary> /// log a single line of text to the log file /// </summary> /// <param name="text">text to log</param> public static void Log(string text) { var frame = new System.Diagnostics.StackTrace().GetFrame(1); var method = frame.GetMethod(); write(string.Format("[{0}] {2}.{3}() [{4}({5},{6})]: {1}", DateTime.Now.ToString("dd.MM.yyyy HH:MM:ss"), text, method.ReflectedType.FullName, method.Name, frame.GetFileName(), frame.GetFileLineNumber(), frame.GetFileColumnNumber())); }
public static void Error(System.Object message, int StackTraceFrameOffset = 0) { var stackTraceFrame = new System.Diagnostics.StackTrace(true).GetFrame(1 + StackTraceFrameOffset); var line = stackTraceFrame.GetFileLineNumber(); var method = stackTraceFrame.GetMethod(); UnityEngine.Debug.LogError(string.Format(LOG_TEXT, method, line, message)); }
private void ReportException(Exception e) { var stack = new System.Diagnostics.StackTrace(e).GetFrame(0); var line = stack.GetFileLineNumber(); var col = stack.GetFileColumnNumber(); var file = stack.GetFileName(); parent.ErrorMessage = string.Format("行{0,-3} 列{1,-3} 错误CCE0002: 比较器发生异常,源文件是 {2},{3}", line, col, file, e.ToString()); }
public static void Log(string format, params object?[]?arg) { var stackFrame = new System.Diagnostics.StackTrace(true).GetFrame(1); var fileName = stackFrame?.GetFileName(); fileName = Path.GetFileName(fileName); var lineNumber = stackFrame?.GetFileLineNumber(); Console.WriteLine($"[{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff zz}] [{fileName}:{lineNumber}] " + format, arg); }
public static Error.IError Create(Error.Level level, string title, string message) { System.Diagnostics.StackFrame frame = new System.Diagnostics.StackTrace().GetFrame(2); System.Reflection.MethodBase method = frame.GetMethod(); Type type = method.DeclaringType; System.Reflection.AssemblyName assembly = type.Assembly.GetName(); return new Entry() { Time = DateTime.Now, Level = level, Title = title, Message = message, AssemblyName = assembly.Name, AssemblyVersion = assembly.Version.ToString(), Type = type.FullName, Method = method.Name, Filename = frame.GetFileName(), Line = frame.GetFileLineNumber(), Column = frame.GetFileColumnNumber(), }; }
// ------------------------------------------------------------------------------------ // Methods // Write to the console public void Write(DebugLevel debugLevel, MessageLevel messageLevel, string message) { if (Application.platform != RuntimePlatform.IPhonePlayer) { System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackTrace(1, true).GetFrame(0); // Get a stack frame from the caller string fileName = stackFrame.GetFileName(); // Get file name of caller string[] fileNameSplit = fileName.Split(new char[] { '\\' }); // Split file name string scriptName = fileNameSplit [fileNameSplit.Length - 1]; // Extract script name from file name string methodName = stackFrame.GetMethod().ToString(); // Get method name of caller int lineNumber = stackFrame.GetFileLineNumber(); // Get line number of caller message = message + System.Environment.NewLine + scriptName + " - " + methodName + " - L" + lineNumber; // Append stack data to message } if ((int)debugLevel <= (int)debug) // Filter messages of higher level than requested { if (playerConsole) // If print all messages in player console { Debug.LogError(message); // Print a LogError } else { switch (messageLevel) // Switch on message level { case MessageLevel.Log: Debug.Log(message); // Print a Log break; case MessageLevel.LogWarning: Debug.LogWarning(message); // Print a LogWarning break; case MessageLevel.LogError: Debug.LogError(message); // Print a LogError break; } } } }
public void Log(string format, params object[] args) { var stackFrame = new System.Diagnostics.StackTrace(true).GetFrame(1); var fileName = stackFrame.GetFileName(); fileName = Path.GetFileName(fileName); var lineNumber = stackFrame.GetFileLineNumber(); string line; if (args.Length == 0) { line = $"[{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff zz}] [{fileName}:{lineNumber}] " + format; } else { line = string.Format($"[{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff zz}] [{fileName}:{lineNumber}] " + format, args); } Console.WriteLine(line); if (LogFile != null) { File.AppendAllText(LogFile, line + "\n"); } }
/// <summary> /// Log a string only once /// </summary> /// <param name="severity">Severity level</param> /// <param name="str">String</param> private static void LogOnce(int severity, string str) { #if UNITY_EDITOR var sf = new System.Diagnostics.StackTrace(true).GetFrame(1); string id = sf.GetFileName() + ":" + sf.GetFileLineNumber(); if (!mSpamCounter.ContainsKey(id) || mSpamCounter[id] == 0) { mSpamCounter[id] = 1; } else { // Already logged once return; } #endif if (severity == 1) { Debug.LogError(str); } else { Debug.Log(str); } }
private string qualifiedObjName() { System.Diagnostics.StackFrame sf = new System.Diagnostics.StackTrace(true).GetFrame(2); string sMethodName = sf.GetMethod().Name; string sClassname = sf.GetMethod().DeclaringType.Name; return string.Format("{0}.{1} Line {2}", sClassname, sMethodName, sf.GetFileLineNumber().ToString()); }
/// <summary> /// Log the message to the log file, provided the logging was /// initialized. /// </summary> /// <param name="message">The message being logged.</param> /// <param name="noTimeStamp">Used to override the default inclusion /// of date/time stamp in each logged line.</param> /// <param name="staticOutputLocation">Used to set console output to a /// fixed location. This is useful for output such as progress /// percentage etc.</param> /// <returns>true if logging was successful, false if not.</returns> public static bool Log(string message, bool noTimeStamp = false, bool staticOutputLocation = false, bool includeStackFrame = false, bool overLoaded = false) { // No logging code should EVER terminate it's parent program // through exceptions. try { // Check if caller wants StackFrame info included. if (includeStackFrame) { int frameOffset = 1; // Check if an overloaded method is used to call Log(). if (overLoaded) { frameOffset++; } // Get the StackFrame including FileInfo. var stackFrame = new System.Diagnostics.StackTrace(true) .GetFrame(frameOffset); // Append the StackFrame info to the message. message = string.Format(message + " --->>> Called from {0} in {1} line {2} column {3}.", stackFrame.GetMethod().ToString(), stackFrame.GetFileName(), stackFrame.GetFileLineNumber(), stackFrame.GetFileColumnNumber()); } // We use a single & because if we're not logging to the // console, it does not matter to what value // staticOutputLocation is set. The use of single & prevents // run time evaluation of the value of staticOutputLocation // if logToConsole is false thus improving runtime execution // performance. if (logToConsole & staticOutputLocation) { // We want static location output so we capture the current // cursor location. _cursorLeft = Console.CursorLeft; _cursorTop = Console.CursorTop; } // Write to the log file. _writer.WriteLine(prependTimeStamp(message, noTimeStamp)); // Flush the writer so no messages are stuck in the buffer. _writer.Flush(); // Check if console output is required. if (logToConsole) { // Are we keeping output in a static location? if (staticOutputLocation) { // Reset the cursor before output. Console.CursorTop = _cursorTop; Console.CursorLeft = _cursorLeft; // Output the message with a large trailing // blank to clear previous output from the static // console line. Console.WriteLine( prependTimeStamp(message, noTimeStamp) + " " + " " + " "); // Reset the cursor after output. Console.CursorTop = _cursorTop; Console.CursorLeft = _cursorLeft; } // No static output location is required. else { // Simply write the message to the console. Console.WriteLine(prependTimeStamp( message, noTimeStamp)); } } if (logToEventLog) { // Write to Event Log's Application log System.Diagnostics.EventLog.WriteEntry("Application", prependTimeStamp(message, noTimeStamp), logToEventLogEntryType); } } // Something went wrong. catch (Exception ex) { System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), logToEventLogEntryType); _outputColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; // Check if output is going to the console. if (logToConsole) { // Write console output, stamped if needed. Console.WriteLine( prependTimeStamp(ex.ToString(), noTimeStamp)); } // No logging code should EVER terminate it's parent program // through exceptions. Wrap this file output in a try/catch // in case file writing throws an exception. try { // Reset the writer to open log file in append mode. using (System.IO.StreamWriter writer = new System.IO.StreamWriter(_logPath, true)) { // Write the exception to the log file, stamped if // needed. writer.WriteLine( prependTimeStamp(ex.ToString(), noTimeStamp)); // Flush the file writer buffer. writer.Flush(); } } // Have to distinguish between inner and outer exceptions. catch (Exception ex2) { System.Diagnostics.EventLog.WriteEntry("Application", ex2.ToString(), logToEventLogEntryType); // Write the inner exception error. Quix.Core.writeConsoleError(ex2.ToString()); } // Reset the console foreground color. Console.ForegroundColor = _outputColor; // Return false since something went wrong during logging. return(false); } // Logging succeeded. return(true); }
public static string StackTraceFileString(int stackFrame) { char seperator = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/'; var stackTrace = new System.Diagnostics.StackTrace(true).GetFrame(stackFrame); string fileString = $"{stackTrace.GetFileName().Split(seperator).Last()}:{stackTrace.GetFileLineNumber()}"; return(fileString); }
private void addSpec(string name, Action operation, bool enabled, SpecType specType) { if (name == String.Empty) { throw new Exception("Please specify a name for the spec"); } var index = 2; var stackFrame = new System.Diagnostics.StackTrace(true).GetFrame(index); var codeBase = stackFrame.GetFileName(); var lineNumber = stackFrame.GetFileLineNumber(); var columnNumber = stackFrame.GetFileColumnNumber(); var fileName = Path.GetFileName(codeBase); var className = this.GetType().FullName; //var className = this.GetType().Name; var spec = Registry.SpecFactory(name, operation, Registry.CurrentSuite, codeBase, lineNumber, columnNumber, fileName, className); spec.Enabled = enabled && spec.Parent.Enabled; Registry.CurrentSuite.AddChild(spec); }