99 lines
3.1 KiB
C#
99 lines
3.1 KiB
C#
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Web;
|
|
namespace RB_MES_API.Controllers
|
|
{
|
|
public static class LogHelper
|
|
{
|
|
private static readonly object writeFile = new object();
|
|
/// <summary>
|
|
/// 在本地写入错误日志
|
|
/// </summary>
|
|
/// <param name="debugstr"></param>
|
|
public static void WriteLog(string debugstr)
|
|
{
|
|
lock (writeFile)
|
|
{
|
|
FileStream fs = null;
|
|
StreamWriter sw = null;
|
|
try
|
|
{
|
|
string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt";
|
|
//服务器中日志目录
|
|
string folder = MapPath("rblog");
|
|
|
|
if (!Directory.Exists(folder))
|
|
Directory.CreateDirectory(folder);
|
|
fs = new FileStream(folder + "/" + filename, FileMode.Append, FileAccess.Write);
|
|
sw = new StreamWriter(fs, Encoding.UTF8);
|
|
sw.WriteLine(DateTime.Now.ToString() + " " + debugstr);
|
|
}
|
|
finally
|
|
{
|
|
if (sw != null)
|
|
{
|
|
sw.Dispose();
|
|
}
|
|
if (fs != null)
|
|
{
|
|
fs.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 在本地写入错误日志
|
|
/// </summary>
|
|
/// <param name="debugstr"></param>
|
|
public static void WriteLog(string debugstr,string name)
|
|
{
|
|
lock (writeFile)
|
|
{
|
|
FileStream fs = null;
|
|
StreamWriter sw = null;
|
|
try
|
|
{
|
|
string filename = DateTime.Now.ToString("yyyyMMdd") + name + ".txt";
|
|
//服务器中日志目录
|
|
string folder = MapPath("rblog");
|
|
|
|
if (!Directory.Exists(folder))
|
|
Directory.CreateDirectory(folder);
|
|
fs = new FileStream(folder + "/" + filename, FileMode.Append, FileAccess.Write);
|
|
sw = new StreamWriter(fs, Encoding.UTF8);
|
|
sw.WriteLine(DateTime.Now.ToString() + " " + debugstr);
|
|
}
|
|
finally
|
|
{
|
|
if (sw != null)
|
|
{
|
|
sw.Dispose();
|
|
}
|
|
if (fs != null)
|
|
{
|
|
fs.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取文件路径
|
|
/// </summary>
|
|
/// <param name="strPath"></param>
|
|
/// <returns></returns>
|
|
private static string MapPath(string strPath)
|
|
{
|
|
strPath = strPath.Replace("/", "\\");
|
|
if (strPath.StartsWith("\\"))
|
|
{
|
|
strPath = strPath.TrimStart('\\');
|
|
}
|
|
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
|
|
|
|
}
|
|
|
|
}
|
|
}
|