locked
An unhandled exception of type 'System.Security.SecurityException' occurred in System.dll RRS feed

  • Question

  • User-1893723813 posted

    Dear All,

    I have a problem about C# programming with visual studio express 2013.

    Here the error:

    An unhandled exception of type 'System.Security.SecurityException' occurred in System.dll

    in code block:

     if (!System.Diagnostics.EventLog.SourceExists("EngineService")) // error when debug this code
                {
                    System.Diagnostics.EventLog.CreateEventSource("EngineService", "EngineServiceLOG");
                }
    
    
    

    how to fix this error.

    best regards,

    Surbakti

    Friday, February 12, 2016 4:21 AM

Answers

  • User-434868552 posted

    @Surbakti

    Your code appears fine, for example, compare it to https://msdn.microsoft.com/en-us/library/System.Diagnostics.EventLog(v=vs.110).aspx:

    // Create the source, if it does not already exist.
    if(!EventLog.SourceExists("MySource"))
    {
         //An event log source should not be created and immediately used.
         //There is a latency time to enable the source, it should be created
         //prior to executing the application that uses the source.
         //Execute this sample a second time to use the new source.
        EventLog.CreateEventSource("MySource", "MyNewLog");
        Console.WriteLine("CreatedEventSource");
        Console.WriteLine("Exiting, execute the application a second time to use the source.");
        // The source is created.  Exit the application to allow it to be registered.
        return;
    }

    HOWEVER, it is absolutely essential that EventLog.CreateEventSource be Run As Administrator.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 12, 2016 4:45 AM

All replies

  • User-434868552 posted

    @Surbakti

    Your code appears fine, for example, compare it to https://msdn.microsoft.com/en-us/library/System.Diagnostics.EventLog(v=vs.110).aspx:

    // Create the source, if it does not already exist.
    if(!EventLog.SourceExists("MySource"))
    {
         //An event log source should not be created and immediately used.
         //There is a latency time to enable the source, it should be created
         //prior to executing the application that uses the source.
         //Execute this sample a second time to use the new source.
        EventLog.CreateEventSource("MySource", "MyNewLog");
        Console.WriteLine("CreatedEventSource");
        Console.WriteLine("Exiting, execute the application a second time to use the source.");
        // The source is created.  Exit the application to allow it to be registered.
        return;
    }

    HOWEVER, it is absolutely essential that EventLog.CreateEventSource be Run As Administrator.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 12, 2016 4:45 AM
  • User303363814 posted

    how to fix this error

    Which part do you want to fix?  The 'unhandled' part or the 'occurred' part?

    If you want to fix the 'unhandled' part then add a try/catch block around the code which is throwing the exception and put a handler in the catch statement.

    If you want to fix the 'occurred' part then you should use the debugger to get more details of why the exception is occurring.  Inspect the Exception. What are its properties?  Are there inner exceptions? What are their properties (including any inner exceptions they might have)?

    Friday, February 12, 2016 4:57 AM