我需要僅在應用程序在調試模式下運行時記錄消息。我找到了2種方法:編譯器指令 - 建議 - 僅在調試模式下運行代碼
第一個:需要在需要記錄日誌時寫入3行。但是,Logger語句在編譯時僅被禁用,這正是我需要的。 Logger.Log根本不會被執行。
#if DEV_ENV
Logger.Log("Application started !"); // This line is grayed. Perfect !
#endif
public static void Log(string message)
{
Debug.WriteLine(message);
}
二:非常整潔。只需要一行代碼即可進行日誌記錄。不知道,是否執行Logger.Log語句。 如果只在編譯時刪除函數調用(與第一種方法相同,但現在確定代碼行不會變灰),我希望這樣做。
Logger.Log("Application started !"); // This line is not grayed out. But, function is not called. So, confused whether its removed at compile time.
[Conditional("DEV_ENV")]
public static void Log(string message)
{
Debug.WriteLine(message);
}
我關心的性能差異。
優秀。得到了答案。 –