How can I disable the function in some class?

I write the log class to trace some result.

using System.Runtime.CompilerServices;

public static class Log
{
public static void WriteLogForTrace(string message, [CallerMemberName] string callerName = null, [CallerFilePath] string sourceFilePath = null,
[CallerLineNumber] int sourceLineNumber = 0)
{
Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss:fff")} \nFile: {sourceFilePath} \nLine Number: {sourceLineNumber} \nCall from:{callerName}
\nmessage: {message} \n");
}
}
It works fine.

Public Class ClassA
{

void test()
   {
            .....
        Log.WriteLogForTrace("test");  
    ......
    }   

}

When I finished tracing ClassA, I want to trace Class B some function.

Public Class ClassB
{

void test1()
   {
            .....
        Log.WriteLogForTrace("test");  
    ......
    }   

}

How can I disable this WriteLogForTrace function in class A in Xamarin Forms ?

If in desktop application, I can use the follow method:

use custom attribute in class A
[LogAttribute(EnableLog:false)]
public Class A
{

}

In Logfunction
WriteLogForTrace()
{
var mth = new StackTrace().GetFrame(1).GetMethod();
var cls = mth.ReflectedType;
var attrs = cls.GetCustomAttributes().OfType();
....
if (EnableLog == false)
{
...
}
else
{
Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss:fff")} \nFile: {sourceFilePath} \nLine Number: {sourceLineNumber} \nCall from:{callerName}
\nmessage: {message} \n");
}
}

Sign In or Register to comment.