using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using CB.Entity;
namespace CB.TrendTool
{
public class LogHelper
{
///
/// 日志的记录类型是Text或SQLServer
///
private static string LogType
{
get
{
return "text";
}
}
///
/// Text日志的存放路径
///
private static string GetLogPath
{
get
{
return System.Configuration.ConfigurationManager.AppSettings["LogPath"].ToString();
}
}
///
/// 记录日志信息
///
/// 彩票种类实体,如果传入null对象将直接将日志存放到统一文件
/// 异常信息
/// 类型(枚举)
/// 是否增加日志的分割线
public static void WriteLog(string MessgeInfo, ExceptionType type, bool WriteTag)
{
if (LogType == "SQLServer")
{
WriteLogToSQLServer(MessgeInfo, type, WriteTag);
}
else //默认都写入日志文件
{
WriteLogToText(MessgeInfo, type, WriteTag);
}
}
///
/// 将日志记录写入文本文件
///
///
///
///
///
private static void WriteLogToText(string MessgeInfo, ExceptionType type, bool WriteTag)
{
string LogPath = "";
if (string.IsNullOrEmpty(GetLogPath))
{
//实体为空记录日志
LogPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "RunLog\\ServiceTooLog\\");
}
else
{
LogPath = GetLogPath;
}
if (!Directory.Exists(LogPath))
{
Directory.CreateDirectory(LogPath);
}
string fileName = LogPath + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
StreamWriter stream = null;
try
{
string log = "[" + System.DateTime.Now.ToString() + "]>> " + MessgeInfo;
if (!File.Exists(fileName))
{
Stream stream2 = null;
try
{
stream2 = File.Create(fileName);
}
finally
{
if (stream2 != null)
{
stream2.Close();
stream2.Dispose();
}
}
}
stream = File.AppendText(fileName);
if (WriteTag)
{
stream.WriteLine("*********************************************************************");
}
stream.WriteLine(log);
}
catch
{ }
finally
{
if (stream != null)
{
stream.Close();
stream.Dispose();
}
}
}
private static void WriteLogToSQLServer( string MessgeInfo, ExceptionType type, bool WriteTag)
{
}
}
public enum ExceptionType
{
///
/// 系统日志
///
SystemLog = 0,
///
/// 异常日志
///
ExceptionLog = 1
}
}