程序中编写log日志

时间:2022-05-21 10:54:50
 public string logFile;
private int configLogLevel = ;
private Stream s = null;
StreamWriter sw = null; /// <summary>
/// 用log文件名初始化对象
/// </summary>
/// <param name="logFile"></param>
public WriteLog(string logFile)
{
this.logFile = logFile;
initFile();
} /// <summary>
/// 用log文件名和配置的log级别初始化对象
/// </summary>
/// <param name="logFile"></param>
/// <param name="configLevel"></param>
public WriteLog(string logFile,int configLevel)
{
this.logFile = logFile;
this.configLogLevel = configLevel;
initFile();
} ~WriteLog()
{
//析构函数,释放资源
//if (sw != null)
//{
// sw.Flush();
// sw.Close();
//}
//if (s != null)
//{
// s.Flush();
// s.Close();
//}
} /// <summary>
/// 初始化文件流
/// </summary>
private void initFile()
{
try
{
if (this.logFile == null || this.logFile == "") return; string path = "";
string fileName = "";
if (this.logFile.IndexOf("\\") >= )
{
path = this.logFile.Substring(,this.logFile.LastIndexOf("\\")+);
fileName = this.logFile.Substring(this.logFile.LastIndexOf("\\")+);
}
else if (this.logFile.IndexOf("/") >= )
{
path = this.logFile.Substring(, this.logFile.LastIndexOf("/")+);
fileName = this.logFile.Substring(this.logFile.LastIndexOf("/") + );
}
else
{
return;
}
string dateStr = DateTime.Now.ToString("yyyyMMdd");
path += dateStr;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
fileName = path + "\\"+ fileName; if (File.Exists(fileName))
{
s = new FileStream(fileName, FileMode.Append, FileAccess.Write);
}
else
{
//CommonFunction.createDir(this.logFile.Substring(0, this.logFile.LastIndexOf(@"\")));
s = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write);
s.Flush();
s.Close();
s = new FileStream(fileName, FileMode.Append, FileAccess.Write);
} sw = new StreamWriter(s, Encoding.Default);//创建Stream流,指定编码方式
//sw.AutoFlush = true;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
/// </summary>
/// <param name="logLevel">当前log的级别</param>
/// <param name="configLevel">配置设定的log级别</param>
/// <param name="logContent">log内容</param>
//[MethodImpl(MethodImplOptions.Synchronized)]
public void log(int logLevel,int configLevel,string logContent)
{
//如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
if (logLevel < configLevel && sw != null && s != null)
{
try
{
lock (sw)
{
sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":logLevel=" + logLevel + " ;configLevel=" + configLevel + " ;" + logContent);
sw.Flush();
//s.Flush();
}
}
catch (Exception ex)
{
ex.ToString();
} }
} /// <summary>
/// 如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
/// </summary>
/// <param name="logLevel">当前log的级别</param>
/// <param name="logContent">log内容</param>
//[MethodImpl(MethodImplOptions.Synchronized)]
public void log(int logLevel, string logContent)
{
//如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
if (logLevel < this.configLogLevel && sw != null && s != null)
{
try
{
lock (sw)
{
sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":logLevel=" + logLevel + " ;configLevel=" + configLogLevel + " ;" + logContent);
sw.Flush();
//s.Flush();
}
}
catch (Exception ex)
{
ex.ToString();
} }
} /// <summary>
/// 如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
/// </summary>
/// <param name="logLevel">当前log的级别</param>
/// <param name="logContent">log内容</param>
/// <param name="method">log抛出调用的方法名</param>
//[MethodImpl(MethodImplOptions.Synchronized)]
public void log(int logLevel, string logContent,string methodName)
{
//如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
if (logLevel < this.configLogLevel && sw != null && s != null)
{
try
{
lock (sw)
{
sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":logLevel=" + logLevel + " ;configLevel=" + configLogLevel + " ;method=" + methodName + ";content=" + logContent);
sw.Flush();
//s.Flush();
}
}
catch (Exception ex)
{
ex.ToString();
}
}
} public void close()
{
try
{
if (sw != null)
{
sw.Flush();
sw.Close();
}
if (s != null)
{
s.Flush();
s.Close();
}
}
catch (Exception ex)
{
ex.ToString();
}
}