| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Text;
- namespace STMes
- {
- public interface ILogService
- {
- void WriteLog(string message);
- }
- public class CoreWriteLogFile : ILogService
- {
- private static object lockObj = new object();
- private string _postfix = "log";
- private bool _isLoging = false;
- public CoreWriteLogFile()
- {
- _postfix = "log";
- }
- public CoreWriteLogFile(string postfix)
- {
- if (postfix.Trim() == "")
- {
- _postfix = "log";
- }
- else
- {
- _postfix = postfix.Trim();
- }
- }
- public void StartLogging(bool startIt)
- {
- _isLoging = startIt;
- }
- #region ILogService ³ÉÔ±
- public void WriteLog(string message)
- {
- WriteLog(message, LogInfoLevel.Message, "", "");
- }
- public void WriteLog(string message, LogInfoLevel level)
- {
- WriteLog(message, level, "", "");
- }
- public void WriteLog(string message, string UserID, string codeInfo)
- {
- WriteLog(message, LogInfoLevel.Message, UserID, codeInfo);
- }
- public void WriteLog(string message, LogInfoLevel level, string UserID, string codeInfo)
- {
- if (!_isLoging) return;
- lock (lockObj)
- {
- try
- {
- string logFile = System.Environment.CurrentDirectory + "/log/" + "DBProxy_" + DateTime.Now.ToString("yyyyMMdd") + "_" + _postfix + ".log";
- FileInfo fi = new FileInfo(logFile);
- StreamWriter sw = null;
- try
- {
- if (fi.Exists)
- sw = fi.AppendText();
- else
- sw = fi.CreateText();
- string str = "";
- if (level == LogInfoLevel.Error)
- str = ">>> Error <<<";
- else
- str = ">>> Message <<<";
- string title = ">>> " + DateTime.Now + " <<<";
- title += str;
- if (UserID != "")
- {
- title += ">>> " + UserID + " <<<";
- }
- if (codeInfo != "")
- {
- title += ">>> " + codeInfo + " <<<";
- }
- sw.WriteLine(title);
- sw.WriteLine("{0}", message);
- sw.WriteLine();
- sw.Flush();
- }
- finally
- {
- if (sw != null)
- sw.Close();
- }
- }
- catch
- {
- // do nothing.
- }
- }
- }
- #endregion
- }
- public enum LogInfoLevel
- {
- Error = 1,
- Message = 2
- }
- }
|