C#最简单好用的日志方法

        /// <summary>
        /// 用来做软件的日常信息保存
        /// </summary>
        /// <param name="strLog"></param>
        public static void InfoLog(string strLog)
        {
            try
            {
                string basepath = AppDomain.CurrentDomain.BaseDirectory + "Log//Info//";
                if (!Directory.Exists(basepath))
                {
                    Directory.CreateDirectory(basepath);
                }

                string path = basepath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                StreamWriter sw = File.AppendText(path);
                sw.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + strLog);
                sw.Close();
            }
            catch
            {
            }
        }

        /// <summary>
        /// 用来做软件的调试信息保存
        /// </summary>
        /// <param name="strLog"></param>
        public static void DebugLog(string strLog)
        {
            try
            {
                string basepath = AppDomain.CurrentDomain.BaseDirectory + "Log//Debug//";
                if (!Directory.Exists(basepath))
                {
                    Directory.CreateDirectory(basepath);
                }

                string path = basepath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                StreamWriter sw = File.AppendText(path);
                sw.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + strLog);
                sw.Close();
            }
            catch
            {
            }
        }

        /// <summary>
        /// 用来做软件的调试信息保存
        /// </summary>
        /// <param name="strLog"></param>
        public static void ErrowLog(string strLog)
        {
            try
            {
                string basepath = AppDomain.CurrentDomain.BaseDirectory + "Log//Errow//";
                if (!Directory.Exists(basepath))
                {
                    Directory.CreateDirectory(basepath);
                }

                string path = basepath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                StreamWriter sw = File.AppendText(path);
                sw.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + strLog);
                sw.Close();
            }
            catch
            {
            }
        }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用日志记录器(Logger)来打印日志。.NET框架中自带了一个日志记录器类库log4net,使用log4net可以实现灵活的日志记录。 以下是一个简单的log4net使用示例: 1. 首先,在项目中引入log4net库,可以通过NuGet方式添加。 2. 在程序入口处初始化log4net: ``` using log4net; using log4net.Config; namespace MyApp { class Program { static void Main(string[] args) { XmlConfigurator.Configure(); // 初始化log4net // ... } } } ``` 3. 定义日志记录器: ``` private static readonly ILog log = LogManager.GetLogger(typeof(MyClass)); ``` 其中typeof(MyClass)表示要记录日志的类名,可以根据需要替换成实际的类名。 4. 在需要记录日志的地方调用日志记录器的方法: ``` log.Debug("This is a debug message."); log.Info("This is an information message."); log.Warn("This is a warning message."); log.Error("This is an error message."); log.Fatal("This is a fatal message."); ``` 以上代码分别打印不同级别的日志信息,具体的级别可以根据需要调整。 5. 最后,在app.config或者web.config文件中添加log4net配置节点: ``` <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/> </layout> </appender> <root> <level value="DEBUG"/> <appender-ref ref="ConsoleAppender"/> </root> </log4net> </configuration> ``` 以上配置会将日志输出到控制台,并且设置了日志级别为DEBUG。可以根据需要调整配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值