SoFunction
Updated on 2025-03-06

C# implements an example of writing exceptions to log (exception log)


/// <summary>
/// Print exception to LOG file
/// </summary>
/// <param name="ex">Exception</param>
/// <param name="LogAddress">Log File Address</param>
public static void WriteLog(Exception ex, string LogAddress = "")
{
//If the log file is empty, create a new YYYY-mm-dd_Log.log file in the Debug directory by default.
    if (LogAddress == "")
    {
        LogAddress = + '\\' +
            + '-' +
            + '-' +
            + "_Log.log";
    }

//Output exception information to the file
    StreamWriter fs = new StreamWriter(LogAddress, true);
("Current time:" + ());
("Exception information:" + );
("Exception object:" + );
("Call stack: \n" + ());
("Trigger method:" + );
    ();
    ();
}