Logger日志打印普通方法
using System;
using System.IO;
using System.Text; namespace Core
{
public class LogHelper
{
private static void writeLog(string log)
{ string strFilePath = AppDomain.CurrentDomain.BaseDirectory + "logs\\log.txt";
string strDirPath = Path.GetDirectoryName(strFilePath);
if (!Directory.Exists(strDirPath))//Directory.Exists(dirPath)目录的路径
Directory.CreateDirectory(strDirPath);//Directory.CreateDirectory(path)创建目录
strFilePath = Path.Combine(strDirPath, "log" + string.Format("{0:yyyyMMdd}", DateTime.Now) + ".txt");//Path.Combine(strpath1,strpath2)
if (!File.Exists(strFilePath))//File.Exists(path),指定路径的文件是否存在
{
//FileStream fsCreate = File.Create(strFilePath);//创建路径
FileStream fsCreate = new FileStream(strFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fsCreate.Close();
}
FileStream fs = new FileStream(strFilePath, FileMode.Append,FileAccess.Write);//任何读取操作都会报异常
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.WriteLine("--------------------------------------------");
sw.WriteLine("Timestamp: "+DateTime.Now.ToString());
sw.WriteLine("Message: " + log);
sw.WriteLine("--------------------------------------------");
sw.Close();
fs.Close();
}
//打印普通信息
public static void WriteInfo(string objectName, string info)
{ writeLog(objectName + ":" + info); } public static void WriteInfo(string objectName, string info, int lines)
{
string strlines = "";
for (int i = ; i < lines; i++)
{
strlines += "\r\n";
}
writeLog(objectName+":"+info);
//writeLog(strlines + DateTime.Now.ToString() + " INFO " + objectName + " " + info);
}
//打印错误信息
public static void WriteError(string objectName, string error)
{
writeLog(objectName+":"+error);
//writeLog(DateTime.Now.ToString() + " ERROR " + objectName + " " + error);
} public static void WriteError(string objectName, string error, int lines)
{
string strlines = "";
for (int i = ; i < lines; i++)
{
strlines += "\r\n";
}
writeLog(objectName + ":" + error);
//writeLog(strlines + DateTime.Now.ToString() + " ERROR " + objectName + " " + error);
} }
}
Logger日志打印普通方法的更多相关文章
- Logger日志打印规范
首先来看一下比较常用的Logger日志级别(部分未列出): error - 运行期错误日志记录,应该有专门的error日志文件.: warn - 警告信息,如程序调用了一个即将作废的接口,接口的不当使 ...
- 深入理解Logger日志——框架绑定原理
深入理解Logger日志--框架绑定原理 说到Logger日志的动态绑定,主要归功与Slf4j,在之前的文章也说过,Slf4j是类似于Apache Common-Logging,英文为Simple L ...
- Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件
Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件 在开发中,我们常常用打印log的方式来调试我们的应用.在Java中我们常常使用方法System.out ...
- Python 日志打印之自定义logger handler
日志打印之自定义logger handler By:授客 QQ:1033553122 #实践环境 WIN 10 Python 3.6.5 #实践代码 handler.py #!/usr/bin/env ...
- 打印 Logger 日志时,需不需要再封装一下工具类?
在开发过程中,打印日志是必不可少的,因为日志关乎于应用的问题排查.应用监控等.现在打印日志一般都是使用 slf4j,因为使用日志门面,有助于打印方式统一,即使后面更换日志框架,也非常方便.在 < ...
- Logger日志级别说明及设置方法、说明 (zhuan)
http://blog.csdn.net/rogger_chen/article/details/50587920 ****************************************** ...
- Logger日志级别说明及设置方法、说明
日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容就是摘自log4j ...
- Python同时向控制台和文件输出日志logging的方法 Python logging模块详解
Python同时向控制台和文件输出日志logging的方法http://www.jb51.net/article/66756.htm 1 #-*- coding:utf-8 -*- 2 import ...
- Java编码常见的Log日志打印问题
前言 本文总结了作者在Java代码检视中遇到的一些关于日志打印的问题,并给出修改建议.因能力有限,难免存在错漏,欢迎指正. 一. 不规范的异常打印 使用slf4j日志组件时,logger.error( ...
随机推荐
- script "text/template"
<script type="text/template" id="orgItem"> <div class="{orgClass}& ...
- 陈朱兴-js写法【案例】:
ajax请求: 一.从服务器端请求数据: var url = '';url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+ ...
- sed 替换
sed -i 's/i=0/i=2/g' test2.sh -i 在当前文档替换 g 替换所有文档 sed -i '3s/cccc/ccccc/' a.txt 将第三行的 cccc 替换成 ccccc ...
- C++ fstream stringstream
一.文件输入输出 C/C++ 输入: freopen("in.cpp", "r", stdin); fclose(stdin); 输出: freopen(&qu ...
- bios启动过程图解
- Windows API调用外部程序
要在应用程序中启动其他的应用程序,有3个函数可以使用,下面我一一说说他们(我以打开D:\Program Files\zeecalls\目录下的zeecalls.exe应用程序为例): 1.Winexe ...
- POJ 2594 传递闭包的最小路径覆盖
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7171 Accepted: 2 ...
- Oracle实现自增方式:序列+触发器
Oracle不能像MySQL那样设置主键自增,Oracle用 <序列+触发器>的方式使数据表的一列或多列实现自增 序列sequence+触发器trigger:实现数据表S_DEPART中的 ...
- 关于oc运行时 isa指针详解
Cocoa框架是iOS应用程序的基础,了解Cocoa框架,对开发iOS应用有很大的帮助. 1.Cocoa是什么? Cocoa是OS X和 iOS操作系统的程序的运行环境. 是什么因素使一个程序成为Co ...
- SharePoint加K2,将Portal系统与BPM系统完美整合!
K2 blackPearl与Microsoft Office SharePoint 一起为解决人员和流程相互合作的解决方案而提供一个强大的平台. K2“blackpearl”根据企业的需求提供了设计, ...