C# 记录日志文件

提供api和第三方对接,由于冻结数据需要传递两位小数,我们这边一直显示的整数,检查代码无误,怀疑对方的传递报文有问题,故在程序中将接收的报文记录日志,方便查看,也便于自己调试

  private static StreamWriter streamWriter; //写文件  

#region 记录postdata
string directPath = "D://MRLog"; //获得文件夹路径,可在web.config中配置 。我懒直接写死了
if (!Directory.Exists(directPath)) //判断文件夹是否存在,如果不存在则创建
{
Directory.CreateDirectory(directPath);
}
directPath += string.Format(@"\{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));
if (streamWriter == null)
{
streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath); //判断文件是否存在如果不存在则创建,如果存在则添加。
}
streamWriter.WriteLine("*******************************************************************");
streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
streamWriter.WriteLine("输出postData报文:");
if (postData != null)
{
streamWriter.WriteLine(postData);
}
if (streamWriter != null)
{
streamWriter.Flush();
streamWriter.Dispose();
streamWriter = null;
}
#endregion

猜你喜欢

转载自www.cnblogs.com/lwlr/p/9109259.html