Wednesday, March 7, 2012

Eventlog

Hello everybody,

is it possible to add entries to Windows EventLog from Sql Server Trigger? if yes, could any body tell me how?

Best Regards

REQUIRES EXTERNAL_ACCESS or UNSAFE CAS Permission Sets on the assembly:

catch (Exception ex)

{

//log failure to local machine's event log

EventLog oLog = new EventLog("Application");

oLog.Source = "usp_ProcessResumes";

oLog.WriteEntry("usp_ProcessResumes failed to execute on " + System.DateTime.Now.ToString() + " The error message was:" + ex.Message.ToString(), EventLogEntryType.Error);

//return error message to client

SqlContext.Pipe.Send(ex.Message.ToString());

}

|||

Hi,

thank you for the prompt reply,

i created a C# dll and tried to create an external procedure to call it, when I call AddEntry method, I get the following exception:

Msg 6522, Level 16, State 1, Procedure EventLogProc, Line 0

A .NET Framework error occurred during execution of user defined routine or aggregate 'EventLogProc':

System.Security.SecurityException: Request for the permission of type 'System.Diagnostics.EventLogPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

System.Security.SecurityException:

at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)

at System.Security.CodeAccessPermission.Demand()

at System.Diagnostics.EventLog.SourceExists(String source, String machineName)

at System.Diagnostics.EventLog.SourceExists(String source)

at EventLogProc.AddEntry(String source, String message, Int32 id, String type)

.

could you please advise?

Regards

|||Look like CAS, I thought External would suffice, sorry bout' that. Try UNSAFE and let me know if that runs or not.|||

I got the following error while create the assemply:

CREATE ASSEMBLY EventLogProc from 'c:\EventLogProc.dll' WITH PERMISSION_SET = UNSAFE

Msg 10327, Level 14, State 1, Line 1

CREATE ASSEMBLY for assembly 'EventLogProc' failed because assembly 'EventLogProc' is not authorized for PERMISSION_SET = UNSAFE. The assembly is authorized when either of the following is true: the database owner (DBO) has UNSAFE ASSEMBLY permission and the database has the TRUSTWORTHY database property on; or the assembly is signed with a certificate or an asymmetric key that has a corresponding login with UNSAFE ASSEMBLY permission.

|||

this is a common error as sql is trying to ensure you are aware of deploying an UNSAFE assembly. To resolve grant the database owner (right click on the database select properties and I believe its options to view the OWNER attribute) the UNSAFE permissions.

GRANT UNSAFE ASSEMBLY TO test;

step 2, run the following code...

ALTER DATABASE [AWSecurity] SET TRUSTWORTHY ON;

then retry....

|||oh yeah...you can use sp_changedbowner if you need to change it to. You may want to just change it to sa to allieveate this issue.|||Is there any C or C++ code example to add event log entry? I need to access windows event log from SQL Server 2000.

No comments:

Post a Comment