Registration Method Design

I am writing a function in C # to register user actions in our system and save them in a database. In particular, I want to register certain functions of business logic as they are called. I came up with a logging method like this:

public static LogMethod(string user, string methodName, object[] parameters, string message)

Inside the method, a function is called for each of the parameters ToString(). For example, for the Foo method, it is called like this:

void Foo(int a, SomeObject b)
{
     Logger.LogMethod(username, "Foo", new object[]{a,b}, "Beginning Foo");
     //etc
}

Is this a good way to register feature calls? What are the best practices for achieving this? Will it affect performance badly?

+3
source share
4 answers

, , , MethodInfo, . , , .

Q & A : #?

+3

- , . , , , .

?

, , . - . , , .

?

, . - - - . ( , , Castle Windsor).

?

. , , , . , , , , , , - . . , , , , .

+3

, AOP - , , .


, . , params.

public static LogMethod(string user, string methodName, string message,
                        params object[] parameters)

.

void Foo(int a, SomeObject b)
{
    Logger.LogMethod(username, "Foo", "Beginning Foo", a, b);
    //etc  
}  
+2

, "" . , .

params, :

public static LogMethod(string user, string methodName, string message, params object[] parameters)

:

Logger.LogMethod(username, "Foo", "Beginning Foo", a, b);

, . . .

, , :

public static LogMethod(string user, string message, params object[] parameters)
{
    string methodName = new StackFrame(1).GetMethod().Name;
    ...      
}

methodName. ( .Net 4.5 CallerMemberNameAttribute.)

; , , , ..

+2

All Articles