using System.Reflection;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.ComponentModel;
using System.Windows.Forms;
using GeoLibrary.Interfaces;
using GeoLibrary.Properties;
namespace GeoLibrary
{
///
/// グローバルハンドラディクショナリクラス
///
/// イベントログ用に生成した動的ハンドラを管理します
///
///
///
#if !DEBUG
[DebuggerStepThrough()]
#endif
public static class GrobalHandlerDictionary
{
///
/// 動的メソッドディクショナリを保持します
///
private static Dictionary
///
/// 同期用オブジェクト
///
private static object s_lockObject;
///
/// 静的コンストラクタ
///
static GrobalHandlerDictionary()
{
GrobalHandlerDictionary.s_dynamicMethodDictionary = new Dictionary
GrobalHandlerDictionary.s_lockObject = new object();
}
///
/// 動的ハンドラ取得メソッド
/// ハンドラタイプ、イベント名、片方でも存在しない場合は例外が発生します
///
/// ハンドラタイプ
/// イベント名
///
public static Delegate GetDynamicHandler(Type type, string eventName)
{
return GrobalHandlerDictionary.s_dynamicMethodDictionary[type][eventName];
}
///
/// 動的ハンドラ生成メソッド
///
/// 引数のイベント情報オブジェクトからイベントハンドラの型を抽出し
/// その型の動的ハンドラを作成します
///
/// 第2引数のログ用ディレクトリ情報オブジェクトの指すディレクトリパスは
/// 動的ハンドラ内で呼び出されるログアウトメソッドに引数として渡されます
///
/// イベントハンドラタイプ
/// イベント名
/// ログ用ディレクトリ情報
public static void CreateDynamicHandler(Type type, string eventName, LogDirectoryInfo logDirectoryInfo)
{
const string C_INVOKE = "Invoke";
const string C_LOGOUTMETHOE = "LogOut";
lock (GrobalHandlerDictionary.s_lockObject)
{
// イベントタイプをチェック
if (type.BaseType != typeof(MulticastDelegate))
{
throw new ArgumentException(Resources.ERR_MES_TYPEOBJECT);
}
// さらにチェック
MethodInfo invoke = type.GetMethod(C_INVOKE);
if (invoke == null)
{
throw new ArgumentException(Resources.ERR_MES_TYPEOBJECT);
}
// 登録されていなければハンドラを生成
if (!GrobalHandlerDictionary.s_dynamicMethodDictionary.ContainsKey(type) ||
GrobalHandlerDictionary.s_dynamicMethodDictionary[type] == null ||
!GrobalHandlerDictionary.s_dynamicMethodDictionary[type].ContainsKey(eventName))
{
// イベントデリゲートの全パラメータを取得
ParameterInfo[] parameters = invoke.GetParameters();
Type[] typeParameters = new Type[parameters.Length];
for (int i = 0; i < dynamicmethod =" new" logoutparameters =" new" logoutmethod =" typeof(GrobalHandlerDictionary).GetMethod(C_LOGOUTMETHOE," path =" logDirectoryInfo.FullName;" ilgenerator =" dynamicMethod.GetILGenerator();">());
}
GrobalHandlerDictionary.s_dynamicMethodDictionary[type].Add(eventName, dynamicMethod.CreateDelegate(type));
}
}
}
///
/// 動的ハンドラ検索メソッド
///
/// ハンドラタイプ
/// ログ用イベント名
///
/// true : 動的ハンドラあり
/// false : 動的ハンドラなし
///
public static bool Contains(Type type, string eventName)
{
if (GrobalHandlerDictionary.s_dynamicMethodDictionary.ContainsKey(type))
{
if (GrobalHandlerDictionary.s_dynamicMethodDictionary[type] != null)
{
if (GrobalHandlerDictionary.s_dynamicMethodDictionary[type].ContainsKey(eventName))
{
if (GrobalHandlerDictionary.s_dynamicMethodDictionary[type][eventName] != null)
{
return true;
}
}
}
}
return false;
}
///
/// ログアウトメソッド
///
/// イベントログを対象にしています
/// 第3・4引数にはイベントハンドラ用の引数をそのまま渡してください
///
/// イベント名
/// ログ用ディレクトリパス
/// sender
/// e
public static void LogOut(string eventName, string directoryPath, object sender, EventArgs e)
{
try
{
string log;
if (e is ILogHolder)
{
log = (e as ILogHolder).Log;
}
else if (sender is Control)
{
log = (sender as Control).Name;
if (sender is CheckBox)
{
log += " " + ((sender as CheckBox).Checked ? "Checked" : "Unchecked");
}
else if (sender is ComboBox)
{
log += "¥nText=¥"" + (sender as ComboBox).Text + "¥" SelectedIndex=" + (sender as ComboBox).SelectedIndex.ToString();
}
else if (sender is TextBox)
{
log += " ¥"" + (sender as TextBox).Text + "¥"";
}
else if (sender is RichTextBox)
{
log += "¥n¥"" + (sender as RichTextBox).Text.TrimEnd(new char[] { '¥r', '¥n' }) + "¥"";
}
else if (sender is System.Windows.Forms.Label)
{
log += " ¥"" + (sender as System.Windows.Forms.Label).Text + "¥"";
}
}
else if (sender != null)
{
log = sender.ToString();
}
else
{
log = "null";
}
// ログをイベントログライタオブジェクトに渡す
XmlEventLogWriter.GetXmlLogWriter(directoryPath).LogWrite(eventName, log);
}
catch
{
Grobal.GrobalErrorHandler(Resources.ERR_MES_LOGOUT, 1, 1);
}
}
}
}
0 件のコメント:
コメントを投稿