情况:
我有一个dll和一个程序,应该使用这个dll与Late绑定。
每个方法都有效,也是事件。例如,有一个记录事件。我想在程序中使用自己的日志记录方法,但这是我的问题。我不怎么做。
dll log class(c#):
/// <summary>
/// log helper class.
/// </summary>
public static class Log {
// ######### Enum #############################################################################################
/// <summary>
/// Log level enum.
/// </summary>
public enum LogLevel {
None = -1,
Debug = 0,
Info = 1,
Warn = 2,
Error = 3,
Fatal = 4
}
// ######### Events ###########################################################################################
/// <summary>
/// log helper class log event.
/// </summary>
public static event EventHandler<HelperLogEventArgs> HelperLog;
// ######### Constructor ######################################################################################
/// <summary>
/// Default constructor
/// </summary>
static Log() {
HelperLog += DebugLog;
}
// ######### Methods ##########################################################################################
/// <summary>
/// log helper class log event handling.
/// </summary>
/// <param name="e">Log event arguments</param>
public static void OnHelperLog(HelperLogEventArgs e) {
EventHandler<HelperLogEventArgs> handler = HelperLog;
if (handler != null) {
handler(null, e);
}
}
/// <summary>
/// Log a message to the debug console.
/// </summary>
/// <param name="sender">Event sender</param>
/// <param name="e">Log event arguments</param>
private static void DebugLog(object sender, HelperLogEventArgs e) {
Debug.WriteLine(String.Format(" [{0}]: {1}", e.logLevel.ToString(), e.message));
}
/// <summary>
/// Log a message.
/// </summary>
/// <param name="level">Log level</param>
/// <param name="message">Log message</param>
public static void Log(LogLevel level, string message) {
HelperLogEventArgs args = new HelperLogEventArgs();
args.logLevel = level;
args.logTime = DateTime.Now;
args.message = message;
OnHelperLog(args);
}
// ######### Help Classes #####################################################################################
/// <summary>
/// log helper class event argument class.
/// </summary>
public class HelperLogEventArgs : EventArgs {
/// <summary>
/// Log message log level.
/// </summary>
public Log.LogLevel logLevel;
/// <summary>
/// Log message text.
/// </summary>
public string message;
/// <summary>
/// Log message log timestamp.
/// </summary>
public DateTime logTime;
}
}
现在我在外部程序中有一个Wrapper类,不幸的是用另一种语言加载了dll(vb.Net。):
If _oAssembly Is Nothing Then
_oAssembly = Assembly.LoadFrom(sTfsHelperDllPath)
End If
' This will not throw an exception but return nothing.
_oTFSHelper = _oAssembly.CreateInstance("TFSHelper.TFSHelper")
If _oTFSHelper Is Nothing Then
' Throw manually an exception on error.
Throw New Exception("## TFSHelper could not be loaded. ##")
End If
现在有人可以告诉我如何编写并在外部程序中添加一个日志记录方法/事件处理程序,然后调用ddl中的事件吗?