123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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
- {
- /// <summary>
- /// 日志的记录类型是Text或SQLServer
- /// </summary>
- private static string LogType
- {
- get
- {
- return "text";
- }
- }
- /// <summary>
- /// Text日志的存放路径
- /// </summary>
- private static string GetLogPath
- {
- get
- {
- return System.Configuration.ConfigurationManager.AppSettings["LogPath"].ToString();
- }
- }
- /// <summary>
- /// 记录日志信息
- /// </summary>
- /// <param name="lt">彩票种类实体,如果传入null对象将直接将日志存放到统一文件</param>
- /// <param name="MessgeInfo">异常信息</param>
- /// <param name="type">类型(枚举)</param>
- /// <param name="WriteTag">是否增加日志的分割线</param>
- public static void WriteLog(string MessgeInfo, ExceptionType type, bool WriteTag)
- {
- if (LogType == "SQLServer")
- {
- WriteLogToSQLServer(MessgeInfo, type, WriteTag);
- }
- else //默认都写入日志文件
- {
- WriteLogToText(MessgeInfo, type, WriteTag);
- }
- }
- /// <summary>
- /// 将日志记录写入文本文件
- /// </summary>
- /// <param name="lt"></param>
- /// <param name="MessgeInfo"></param>
- /// <param name="type"></param>
- /// <param name="WriteTag"></param>
- 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
- {
- /// <summary>
- /// 系统日志
- /// </summary>
- SystemLog = 0,
- /// <summary>
- /// 异常日志
- /// </summary>
- ExceptionLog = 1
- }
- }
|