虽然对Dephi不了解,对多线程也不太了解,但是这个还比较好用,安全性也不错,记录日志用特定格式保存,指定的软件才能打开,记下了。
接口类
Code
//c#调用Dephi
public static class DrawChartFromDll
{
//function set_save_path(aDir : PChar) : Cardinal; stdcall;
//function log_message(aCode : integer; aBuffer : pointer; aBufferSize: DWORD) : Cardinal; stdcall;
//function log_error(aCode : integer; aBuffer : pointer; aBufferSize: DWORD) : Cardinal; stdcall;
//function save: Cardinal; stdcall;
//function set_active(aValue : LongBool): Cardinal; stdcall;
//调用非托管Dll,GetChartFromXMLByNet是ChartAccess.dll公开的函数名称
[DllImport("log.dll", EntryPoint = "set_save_path", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//设置保存路径 (D:\)
public static extern void set_save_path(string path);
[DllImport("log.dll", EntryPoint = "log_message", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//日志信息
public static extern int log_message(int Code, IntPtr aBuffer, int aBufferSize);
[DllImport("log.dll", EntryPoint = "log_error", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//日志错误信息
public static extern int log_error(int Code, IntPtr aBuffer, int aBufferSize);
[DllImport("log.dll", EntryPoint = "save", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//保存方法
public static extern int save();
[DllImport("log.dll", EntryPoint = "set_active", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//是否启动日志功能
public static extern int set_active(bool value);
//c#调用Dephi
public static class DrawChartFromDll
{
//function set_save_path(aDir : PChar) : Cardinal; stdcall;
//function log_message(aCode : integer; aBuffer : pointer; aBufferSize: DWORD) : Cardinal; stdcall;
//function log_error(aCode : integer; aBuffer : pointer; aBufferSize: DWORD) : Cardinal; stdcall;
//function save: Cardinal; stdcall;
//function set_active(aValue : LongBool): Cardinal; stdcall;
//调用非托管Dll,GetChartFromXMLByNet是ChartAccess.dll公开的函数名称
[DllImport("log.dll", EntryPoint = "set_save_path", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//设置保存路径 (D:\)
public static extern void set_save_path(string path);
[DllImport("log.dll", EntryPoint = "log_message", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//日志信息
public static extern int log_message(int Code, IntPtr aBuffer, int aBufferSize);
[DllImport("log.dll", EntryPoint = "log_error", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//日志错误信息
public static extern int log_error(int Code, IntPtr aBuffer, int aBufferSize);
[DllImport("log.dll", EntryPoint = "save", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//保存方法
public static extern int save();
[DllImport("log.dll", EntryPoint = "set_active", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
//是否启动日志功能
public static extern int set_active(bool value);
ASP.NET实现:
Code
public class LogInput
{
public LogInput()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 设置保存路径 (D:\)
/// </summary>
/// <param name="path"></param>
public static void set_save_path(string path) {
DrawChartFromDll.set_save_path(path);
}
/// <summary>
/// 日志信息
/// </summary>
/// <param name="Code"></param>
/// <param name="message"></param>
public static void log_message(int Code, string message)
{
DrawChartFromDll.log_message(Code, Marshal.StringToHGlobalAnsi(message), System.Text.Encoding.Default.GetByteCount(message));
}
/// <summary>
/// 日志错误信息
/// </summary>
/// <param name="Code"></param>
/// <param name="message"></param>
public static void log_error(int Code, string message)
{
DrawChartFromDll.log_message(Code, Marshal.StringToHGlobalAnsi(message), System.Text.Encoding.Default.GetByteCount(message));
}
/// <summary>
/// 保存方法
/// </summary>
public static void save() {
DrawChartFromDll.save();
}
/// <summary>
/// 是否启动日志功能
/// </summary>
/// <param name="value"></param>
public static void set_active(bool value) {
DrawChartFromDll.set_active(value);
}
}
public class LogInput
{
public LogInput()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 设置保存路径 (D:\)
/// </summary>
/// <param name="path"></param>
public static void set_save_path(string path) {
DrawChartFromDll.set_save_path(path);
}
/// <summary>
/// 日志信息
/// </summary>
/// <param name="Code"></param>
/// <param name="message"></param>
public static void log_message(int Code, string message)
{
DrawChartFromDll.log_message(Code, Marshal.StringToHGlobalAnsi(message), System.Text.Encoding.Default.GetByteCount(message));
}
/// <summary>
/// 日志错误信息
/// </summary>
/// <param name="Code"></param>
/// <param name="message"></param>
public static void log_error(int Code, string message)
{
DrawChartFromDll.log_message(Code, Marshal.StringToHGlobalAnsi(message), System.Text.Encoding.Default.GetByteCount(message));
}
/// <summary>
/// 保存方法
/// </summary>
public static void save() {
DrawChartFromDll.save();
}
/// <summary>
/// 是否启动日志功能
/// </summary>
/// <param name="value"></param>
public static void set_active(bool value) {
DrawChartFromDll.set_active(value);
}
}
调用:
Code
PDL.Common.LogInput.set_active(true);
PDL.Common.LogInput.set_save_path(Server.MapPath("\\"));
PDL.Common.LogInput.log_message(0, "日志功能启动");
Response.Write("Start Success");
PDL.Common.LogInput.log_message(0, "日志功能关闭");
PDL.Common.LogInput.save();
PDL.Common.LogInput.set_active(false);
Response.Write("Shut Success");
PDL.Common.LogInput.set_active(true);
PDL.Common.LogInput.set_save_path(Server.MapPath("\\"));
PDL.Common.LogInput.log_message(0, "日志功能启动");
Response.Write("Start Success");
PDL.Common.LogInput.log_message(0, "日志功能关闭");
PDL.Common.LogInput.save();
PDL.Common.LogInput.set_active(false);
Response.Write("Shut Success");