- using System;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Web;
- namespace CommonPrj
- {
- public class LogAccess
- {
- private static StreamWriter objLogWrite;
- private static Object thisLock = new Object();
- public static void WriteLog(String p_OperationFlag)
- {
- StackTrace st = new StackTrace(true);
- String strClassName; //クラスの名前
- String strMethodName; //関数の名前
- String strLogMsg = String.Empty; //ログファイルの内容
- String strIPAddress = ""; //IPアドレス
- String strUserName = ""; //操作者
- try
- {
- lock (thisLock)
- {
- //セクションがない場合、戻る。
- if (HttpContext.Current.Session["objSession"] == null)
- {
- return;
- }
- LoginSession objUserInfo = (LoginSession)HttpContext.Current.Session["objSession"];
- strIPAddress = objUserInfo.Adress;
- strUserName = objUserInfo.UserName;
- //メッソドがある場合、ログの内容をセット
- if (st.GetFrame(1).GetMethod() != null)
- {
- strClassName = st.GetFrame(1).GetMethod().DeclaringType.Name + ".cs";
- strMethodName = st.GetFrame(1).GetMethod().Name;
- strLogMsg = "; " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "; " + strIPAddress +
- "; " + strUserName + "; " + strClassName + "; " + strMethodName + "; " + p_OperationFlag;
- }
- //ファイル指定
- FileInfo objLogFile = new FileInfo(WebConfig.appLogFilePath);
- //フォルダーがない場合、フォルダを新規作成
- if (objLogFile.Directory.Exists == false)
- {
- objLogFile.Directory.Create();
- }
- //ファイルがない場合、ファイルを新規
- if (objLogFile.Exists == false)
- {
- objLogWrite = objLogFile.CreateText();
- }
- //ファイルがある場合、ファイルを追加
- else
- {
- objLogWrite = objLogFile.AppendText();
- }
- //ファイルを作成
- objLogWrite.WriteLine(strLogMsg);
- objLogWrite.Flush();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- //ファイルをクローズ
- if (objLogWrite != null)
- {
- objLogWrite.Close();
- objLogWrite = null;
- }
- }
- }
- public static void WriteErrLog(String p_Msg)
- {
- StackTrace st = new StackTrace(true);
- String strClassName; //クラスの名前
- String strMethodName; //関数の名前
- String strErrMsg = String.Empty; //ログファイルの内容
- String strIPAddress = ""; //IPアドレス
- String strUserName = ""; //操作者
- try
- {
- lock (thisLock)
- {
- //セクションがある場合、セクションからログイン情報を取得する。
- if (HttpContext.Current.Session["objSession"] == null)
- {
- return;
- }
- LoginSession objUserInfo = (LoginSession)HttpContext.Current.Session["objSession"];
- strIPAddress = objUserInfo.Adress;
- strUserName = objUserInfo.UserName;
- //メッソドがある場合、エラーメッセージの内容をセット
- if (st.GetFrame(1).GetMethod() != null)
- {
- strClassName = st.GetFrame(1).GetMethod().DeclaringType.Name + ".cs";
- strMethodName = st.GetFrame(1).GetMethod().Name;
- strErrMsg = "; " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "; " + strIPAddress +
- "; " + strUserName + "; " + strClassName + "; " + strMethodName + "; " + p_Msg;
- }
- //ファイル指定
- FileInfo objLogFile = new FileInfo(WebConfig.appErrLogPath);
- //フォルダーがない場合、フォルダを作成
- if (objLogFile.Directory.Exists == false)
- {
- objLogFile.Directory.Create();
- }
- //ファイルがない場合、ファイルを新規作成
- if (objLogFile.Exists == false)
- {
- objLogWrite = objLogFile.CreateText();
- }
- //ファイルがある場合、ファイルを追加
- else
- {
- objLogWrite = objLogFile.AppendText();
- }
- //ファイルを作成
- objLogWrite.WriteLine(strErrMsg);
- objLogWrite.Flush();
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- finally
- {
- //ログファイルがある場合、ファイルをクローズ
- if (objLogWrite != null)
- {
- objLogWrite.Close();
- objLogWrite = null;
- }
- }
- }
- }
- }
日志处理类(Log)
最新推荐文章于 2019-09-03 10:26:35 发布