Parse c to see the solution to the current call stack without an exception

  • 2020-05-10 18:45:55
  • OfStack

C# view stack is usually used in exception handling. After the exception occurs, it is very convenient to get the code call path of the error through the exception stack. This is useful. Can you use this method to troubleshoot non-exceptional errors when there are no exceptions? The answer is yes.
Cause:
There are several ways to post in the forum, which may be posts directly imported by the news system, or posts captured by users, or posts posted by users in a normal way. However, there is a problem in the past two days. Some posts have the wrong HasImage attribute. Debugging through several methods can not reproduce the problem, there is no way, only in the program to add a reply to add a logger to record the stack, so as to trace which way is the problem of the post.
Code:

[PostProviderExtension] 
public class HasImageErrorCheckerPostExtension : IPostProviderExtension 
{ 
    public void BindEvents(PostProviderBase postProvider) 
    { 
        postProvider.Added += new PostChanged(postProvider_Added); 
    } 

    void postProvider_Added(Model.PostInfo post) 
    { 
        try
        { 
            StackFrame[] stacks = new StackTrace().GetFrames(); 
            if (post.Content.IndexOf("IMG") > -1 && post.HasImage == false) 
            { 
                StringBuilder sb = new StringBuilder(); 
                sb.AppendLine(" Problems arise "); 
                sb.AppendLine("stack is:"); 
                sb.Append(ToString(stacks)); 

                sb.Append("content="); 
                sb.AppendLine(post.Content); 

                sb.Append("HasImage="); 
                sb.AppendLine(UserPostContentProcessor.HasImage(post.Content).ToString()); 

                sb.Append("createUserID="); 
                sb.AppendLine(post.CreateUserID.ToString()); 
                sb.AppendLine(string.Format("LoginUser={0},Level={1}",PageBase.GetLoginUser().ID,PageBase.GetLoginUser().LevelNo)); 

                TextLogWriter.NamedInstance("\\log\\HasImageErrorCheckerPostExtension\\").Write(sb.ToString()); 
            } 
        } 
        catch (Exception ex) 
        {  
            TextLogWriter.NamedInstance("\\log\\HasImageErrorCheckerPostExtension\\").Write(ex); 
        } 
    } 

    private string ToString(StackFrame[] stacks) 
    { 
        string result = string.Empty; 
        foreach (StackFrame stack in stacks) 
        { 
            result += string.Format("{0} {1} {2} {3}\r\n", stack.GetFileName(), 
                stack.GetFileLineNumber(), 
                stack.GetFileColumnNumber(), 
                stack.GetMethod().ToString()); 
        } 
        return result; 
    } 
}

The above class HasImageErrorCheckerPostExtension inherits from IPostProviderExtension and is decorated with the PostProviderExtension property, which is automatically called by the system and triggers the events bound here when Posting. The core code here is new StackTrace().GetFrames(), which gives you the stack information for the current program execution. The method name can be obtained in Release mode, and the file line number and column number can be obtained in Debug mode.
This method is one of the options for finding problems when you cannot reproduce them in debugging.

Related articles: