3

when i compile the following code it shows the error as system.securityexception error.

using System;    
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace eventlog
{
   class MySample{

public static void Main(){

   if(!EventLog.SourceExists("MySource"))
    {            
        EventLog.CreateEventSource("MySource", "MyNewLog");
        Console.WriteLine("CreatedEventSource");
        Console.WriteLine("Exiting, execute the application a second time to use the source.");
        return;
    }        EventLog myLog = new EventLog();
    myLog.Source = "MySource";
   myLog.WriteEntry("Writing to event log.");

}

} }

how to fix this error

3
  • I copied your code and it compiles and runs well. What are the details of the exception? Commented Feb 1, 2013 at 10:06
  • Unhandled Exception: System.Security.SecurityException: The source was not found , but some or all event logs could not be searched. Inaccessible logs: Security . at System.Diagnostics.EventLog.FindSourceRegistration(String source, String m achineName, Boolean readOnly) at System.Diagnostics.EventLog.SourceExists(String source, String machineName ) at eventlog.MySample.Main() in E:\.net prep\.net examples\eventlog\eventlog\P rogram.cs:line 13 The Zone of the assembly that failed was: MyComputer Press any key to continue . . . Commented Feb 1, 2013 at 10:08
  • please look at this question stackoverflow.com/questions/3622396/… Commented Feb 1, 2013 at 10:11

3 Answers 3

3

I think you must have Admin privilege for creating new EventLog.

Sign up to request clarification or add additional context in comments.

5 Comments

how to get that admin privilege
1. With Manifest file (msdn.microsoft.com/en-us/library/bb756929.aspx) - 2. Right Click on Exe (Run as Administrator) 3. Shortcut to exe, Property of shortcut (Run as Administrator)
sorry to downvote but your answer is misleading. you CAN write to the EventLog without Admin privileges. You just cannot do what his code example does without admin privilege. See my "answer" (which I know does not completely answer how to do what he wants without admin privileges, but is a solution for somebody who just wants to write to it)
Have you read: CREATING not writing. Try yourself to create a NEW EventLog without Admin privilege.... And by the way this not a race everyone can post an answer that can be contribute to the solution.
2

Read this article: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx

That is why everybody says not write to eventlog with impersonation. You have to put the user or user group into the local Administrator group whitch is not advised. The service user could have this privilege.

Comments

0

To be more explicit, right click the application (exe) and choose "Run as Administrator"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.