VB.NET trace variables

I am working on an approach to the tracing protocol for my company’s project. The VB.NET..NET Framework has fairly universal tracing capabilities, and I would like to use what .NET already provides. My company wants to stay away from third-party software, which is why log4net is out of the question.

They want to be able to track the flow of the web application, and using trace sources, listeners, and switches will make this part pretty easy. However, they want me to keep track of when variables are changing throughout the program, without taking into account each Trace.Write trace ("i =" and i).

So, are there any effective approaches for this?

Your answers are welcome. Thanks in advance.


I decided to go with the class. I just created a TraceVariable Class in which there was an IntegerChanged event. Thus, developers of other parts of the code will be able to control how to handle the change of a variable if it wants to do something other than tracking.

Here is the code:

Public Class TraceVariable  
    Private mInteger As Integer  
    Public Event IntegerChanged(ByVal mInteger As Integer)  
    Public Property TraceInteger() As Integer
        Get
            TraceInteger = mInteger
        End Get
        Set(ByVal value As Integer)
            mInteger = value
            RaiseEvent IntegerChanged(mInteger)
        End Set
    End Property
End Class

Thanks for answers! Regarding the fact that this is random, we will use it only for critical variables, so do not worry. Tracking in our situation is a necessity and precaution.

+3
source share
4 answers

Tracking the changes of each variable will quickly become erratic and lead to information overload for any, but the most trivial applications.

, , Traceable , Trace .

+2

, int, double .., , , , ().

, , - AOP (, postharp) IL- () , , .

0

, , ... , , .

, . , , .

0

(warning = > #):

    class Program
{
    static void Main(string[] args)
    {
        test(1, "one");
        test(2, "two");
        test(3, "three");
        Console.ReadLine();
    }

    private static void test(int x, string y)
    {
        DisplayParameterNameAndValue(() => x);
        DisplayParameterNameAndValue(() => y);
    }


    static void DisplayParameterNameAndValue(Expression<Func<object>> parameter)
    {
        var expressionBody = parameter.Body;
        MemberExpression memberExpression;
        if (expressionBody is UnaryExpression)
        {
            memberExpression = (MemberExpression)((UnaryExpression)expressionBody).Operand;
        }
        else if (expressionBody is MemberExpression)
        {
            memberExpression = (MemberExpression)expressionBody;
        }
        else
        {
            throw new ArgumentException("parameter");
        }
        string parameterName = memberExpression.Member.Name;
        object parameterValue = parameter.Compile().Invoke();
        Console.WriteLine("{0} => {1}", parameterName, parameterValue);
    }
}

VB :

Module Module1

Sub Main()
    test(1, "one")
    test(2, "two")
    test(3, "three")
    Console.ReadLine()
End Sub
Private Sub test(ByVal x As Integer, ByVal y As String)
    DisplayParameterNameAndValue(Function() x)
    DisplayParameterNameAndValue(Function() y)
End Sub


Private Sub DisplayParameterNameAndValue(ByVal parameter As Expression(Of Func(Of Object)))
    Dim expressionBody = parameter.Body
    Dim memberExpression As MemberExpression
    If TypeOf expressionBody Is UnaryExpression Then
        memberExpression = DirectCast(DirectCast(expressionBody, UnaryExpression).Operand, MemberExpression)
    ElseIf TypeOf expressionBody Is MemberExpression Then
        memberExpression = DirectCast(expressionBody, MemberExpression)
    Else
        Throw New ArgumentException("parameter")
    End If
    Dim parameterName As String = memberExpression.Member.Name
    Dim parameterValue As Object = parameter.Compile().Invoke()
    Console.WriteLine("{0} => {1}", parameterName, parameterValue)
  End Sub
End Module

, , #, bad vb!

, .

0

Source: https://habr.com/ru/post/1702928/


All Articles