事件记录到数据库和事件查看器

问题描述:

using System; 
using System.Threading.Tasks; 
using System.Data; 
using System.Data.Common; 
using System.Data.SqlClient; 
using System.DirectoryServices; 
using Microsoft.Practices.EnterpriseLibrary.Logging; 
using Microsoft.Practices.EnterpriseLibrary.Common; 
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; 
using Microsoft.Practices.EnterpriseLibrary.Data; 
using System.Diagnostics; 
using System.Configuration; 

namespace DB_Logger_1 
{ 

/*This method for Logging to DB*/ 
public class DBLogger 
{ 
private Database dbClient = null; 
SqlConnection conn = new SqlConnection(); 
public void Log(string UserName, int EventID, string Message, TraceEventType Severity, int Priority) 
{ 
conn.ConnectionString = @"Data Source=D-DG2H6BS\MSSQLSERVER2014;Initial Catalog=DummyDB1;User ID=sa;Password=***********;"; 
conn.Open(); 
using (DbCommand cmdInsertLog = dbClient.GetStoredProcCommand("LogToDatabase")) 
{ 
try 
{ 
dbClient.AddInParameter(cmdInsertLog, "UserName", DbType.String, UserName); 
dbClient.AddInParameter(cmdInsertLog, "EventID", DbType.Int32, EventID); 
dbClient.AddInParameter(cmdInsertLog, "Message", DbType.String, Message); 
dbClient.AddInParameter(cmdInsertLog, "TraceEventType", DbType.String, Severity); 
dbClient.AddInParameter(cmdInsertLog, "Priority", DbType.String, Priority); 
dbClient.ExecuteNonQuery(cmdInsertLog); 
} 
catch (Exception ex) 
{ 
throw ex; 
} 
finally 
{ 
if (cmdInsertLog.Connection.State == ConnectionState.Open) 
{ 
cmdInsertLog.Connection.Close(); 
} 
cmdInsertLog.Dispose(); 
} 
} 
} 
} 
public class EventLogger 
{ 
public void Log(string UserName, int EventID, string Message, TraceEventType Severity, int Priority) 
{ 
LogEntry logEntry = new LogEntry(); 
logEntry.Title = Message; 
logEntry.EventId = EventID; 
logEntry.Message = Message; 
logEntry.TimeStamp = DateTime.Now; 
} 
} 
} 

这里有什么错误......这段代码是否可以将事件记录到数据库和事件查看器中?提供解决方案。这个logtoDB部分可能包含错误。我还没有检查它。纠正错误,如果有的话,并张贴我相同或建议我的解决方案与此代码只。事件记录到数据库和事件查看器

+0

你应该说明你得到了什么错误信息。这不是一项测试服务。 – Daz

+0

“程序不包含适用于入口点的静态主要方法”是错误消息。此解决方案是否有效? – Naveenkumar

+0

该错误消息意味着Visual Studio不知道如何开始运行代码。例如,如果你正在创建一个命令行程序,你会提供一个Main()函数作为入口点。这与上面的代码无关。 – Daz

可能更好的决定是使用一个现成的“记录器库”,例如log4net例如? 您可以使用它简单MS SQL logging and and Event Viewer

Here一步一步的指令(或如果你知道俄罗斯 - here with screenshots)。