日志处理类(Log)

  1. using System;
  2. using System.Data;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Web;
  6. namespace CommonPrj
  7. {
  8.     public class LogAccess
  9.     {
  10.         private static StreamWriter objLogWrite;
  11.         private static Object thisLock = new Object();
  12.         public static void WriteLog(String p_OperationFlag)
  13.         {
  14.             StackTrace st = new StackTrace(true);
  15.             String strClassName;                        //クラスの名前
  16.             String strMethodName;                       //関数の名前
  17.             String strLogMsg = String.Empty;            //ログファイルの内容
  18.             String strIPAddress = "";                   //IPアドレス
  19.             String strUserName = "";                    //操作者
  20.             try
  21.             {
  22.                 lock (thisLock)
  23.                 {
  24.                     //セクションがない場合、戻る。
  25.                     if (HttpContext.Current.Session["objSession"] == null)
  26.                     {
  27.                         return;
  28.                     }
  29.                     LoginSession objUserInfo = (LoginSession)HttpContext.Current.Session["objSession"];
  30.                     strIPAddress = objUserInfo.Adress;
  31.                     strUserName = objUserInfo.UserName;
  32.                     //メッソドがある場合、ログの内容をセット
  33.                     if (st.GetFrame(1).GetMethod() != null)
  34.                     {
  35.                         strClassName = st.GetFrame(1).GetMethod().DeclaringType.Name + ".cs";
  36.                         strMethodName = st.GetFrame(1).GetMethod().Name;
  37.                         strLogMsg = "; " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "; " + strIPAddress +
  38.                             "; " + strUserName + "; " + strClassName + "; " + strMethodName + "; " + p_OperationFlag;
  39.                     }
  40.                     //ファイル指定
  41.                     FileInfo objLogFile = new FileInfo(WebConfig.appLogFilePath);
  42.                     //フォルダーがない場合、フォルダを新規作成
  43.                     if (objLogFile.Directory.Exists == false)
  44.                     {
  45.                         objLogFile.Directory.Create();
  46.                     }
  47.                     //ファイルがない場合、ファイルを新規
  48.                     if (objLogFile.Exists == false)
  49.                     {
  50.                         objLogWrite = objLogFile.CreateText();
  51.                     }
  52.                     //ファイルがある場合、ファイルを追加
  53.                     else
  54.                     {
  55.                         objLogWrite = objLogFile.AppendText();
  56.                     }
  57.                     //ファイルを作成
  58.                     objLogWrite.WriteLine(strLogMsg);
  59.                     objLogWrite.Flush();
  60.                 }
  61.             }
  62.             catch (Exception ex)
  63.             {
  64.                 throw ex;
  65.             }
  66.             finally
  67.             {
  68.                 //ファイルをクローズ
  69.                 if (objLogWrite != null)
  70.                 {
  71.                     objLogWrite.Close();
  72.                     objLogWrite = null;
  73.                 }
  74.             }
  75.         }
  76.         public static void WriteErrLog(String p_Msg)
  77.         {
  78.             StackTrace st = new StackTrace(true);
  79.             String strClassName;                        //クラスの名前
  80.             String strMethodName;                       //関数の名前
  81.             String strErrMsg = String.Empty;            //ログファイルの内容
  82.             String strIPAddress = "";                   //IPアドレス
  83.             String strUserName = "";                    //操作者
  84.             try
  85.             {
  86.                 lock (thisLock)
  87.                 {
  88.                     //セクションがある場合、セクションからログイン情報を取得する。
  89.                     if (HttpContext.Current.Session["objSession"] == null)
  90.                     {
  91.                         return;
  92.                     }
  93.                     LoginSession objUserInfo = (LoginSession)HttpContext.Current.Session["objSession"];
  94.                     strIPAddress = objUserInfo.Adress;
  95.                     strUserName = objUserInfo.UserName;
  96.                     //メッソドがある場合、エラーメッセージの内容をセット
  97.                     if (st.GetFrame(1).GetMethod() != null)
  98.                     {
  99.                         strClassName = st.GetFrame(1).GetMethod().DeclaringType.Name + ".cs";
  100.                         strMethodName = st.GetFrame(1).GetMethod().Name;
  101.                         strErrMsg = "; " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "; " + strIPAddress +
  102.                             "; " + strUserName + "; " + strClassName + "; " + strMethodName + "; " + p_Msg;
  103.                     }
  104.                     //ファイル指定
  105.                     FileInfo objLogFile = new FileInfo(WebConfig.appErrLogPath);
  106.                     //フォルダーがない場合、フォルダを作成
  107.                     if (objLogFile.Directory.Exists == false)
  108.                     {
  109.                         objLogFile.Directory.Create();
  110.                     }
  111.                     //ファイルがない場合、ファイルを新規作成
  112.                     if (objLogFile.Exists == false)
  113.                     {
  114.                         objLogWrite = objLogFile.CreateText();
  115.                     }
  116.                     //ファイルがある場合、ファイルを追加
  117.                     else
  118.                     {
  119.                         objLogWrite = objLogFile.AppendText();
  120.                     }
  121.                     //ファイルを作成
  122.                     objLogWrite.WriteLine(strErrMsg);
  123.                     objLogWrite.Flush();
  124.                 }
  125.             }
  126.             catch (Exception e)
  127.             {
  128.                 throw e;
  129.             }
  130.             finally
  131.             {
  132.                 //ログファイルがある場合、ファイルをクローズ
  133.                 if (objLogWrite != null)
  134.                 {
  135.                     objLogWrite.Close();
  136.                     objLogWrite = null;
  137.                 }
  138.             }
  139.         }
  140.     }
  141. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值