C# 日志类


using System;
using System.IO;

namespace
{
    /* *
     * Revision
     * 2022-02-17      New
     * */

    /// 
    /// This class is to show log message in the console, and write the message into log file.
    /// It contains 5 log levels for different usage. 
    /// 
    public static class Logging
    {
        /// 
        /// Log level:
        /// 0   Debug
        /// 1   Info
        /// 2   Warn
        /// 3   Error
        /// 4   Fatal
        /// 5   Internal
        /// If logging level is higher than the requsted logging level, the message will not be shown and recorded.
        /// 
        public static LoggingLevel Level { get; set; } = 0;

        /// 
        /// Log folder path. When it is set, the log file path is set. 
        /// 
        public static string FolderPath { get; set; }

        /// 
        /// Log file path, it will be set according to the folder path and the current date.
        /// 
        private static string filePath = ".log";

        public static void SetFilePath()
        {
            filePath = Path.Combine(FolderPath, $"{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.log");
        }

        /// 
        /// Show the log and record it in the file
        /// 
        /// 
        /// 
        public static void Log(LoggingLevel level, string message)
        {
            string formattedMessage;

            // if the requested level is greater than the setting, the message will be shown and recorded.
            if (level >= Level)
            {
                formattedMessage = $"[{DateTime.Now}]\t{level.ToString().ToUpper()}\t{message}";

                if (level != LoggingLevel.Internal)
                {
                    // If the logging level is internal, the message will not be shown. 
                    Console.WriteLine(formattedMessage);
                }
                else
                {
                    // TODO
                }

                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    // If log folder path is not set, log file path is emtpy.
                    // Then the message will not be recorded.

                    try
                    {
                        File.AppendAllText(filePath, formattedMessage + "\r\n");
                    }
                    catch (IOException ioe)
                    {
                        // The most probabe exception could be the folder path does not exists.
                        throw new IOException($"An exception occurs: {ioe.Message}");
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"An unexpected exception occurs: {ex.Message}");
                    }
                }
                else
                {
                    // TODO
                }
            }
            else
            {
                // TODO
            }
        }

        public static void Debug(string message)
        {
            // Logging log for debug information message.
            Log(LoggingLevel.Debug, message);
        }

        public static void Info(string message)
        {
            // Logging log for general information message.
            Log(LoggingLevel.Info, message);
        }

        public static void Warn(string message)
        {
            // Logging log for warning message.
            Log(LoggingLevel.Warn, message);
        }

        public static void Error(string message)
        {
            // Logging log for general error message.
            Log(LoggingLevel.Error, message);
        }

        public static void Fatal(string message)
        {
            // Logging log for fatal error message.
            Log(LoggingLevel.Fatal, message);
        }

        public static void Internal(string message)
        {
            // Logging log for internal message.
            Log(LoggingLevel.Internal, message);
        }
    }

    public enum LoggingLevel
    {
        Debug = 0,
        Info,
        Warn,
        Error,
        Fatal,
        // Internal level is only to record the log, not shown.
        Internal
    }
}