Locked CA2214 while setting EventLog.Source in Windows Service

  • Monday, August 06, 2012 1:51 PM
     
      Has Code

    I get this warnig, from my Windows service code. I am not sure what is wrong with it. EventLog source is done by exmaples on web.  Can I safely supress message? Is any other suitable place for seting source other than constructor? 

    Warning 1 CA2214 : Microsoft.Usage : 'Service.Service()' contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences: 


    Service..ctor()
    Service.InitializeComponent():Void
    Serv
    ServiceBase.get_EventLog():EventLog D:\Users\lnemec\Documents\Visual Studio 2010\Projects\SiriusMETS\SiriusBatchProcessor\SiriusBatchProcessor\Service.Designer.cs 8 Service

    Code samble

    public Service()

    {         InitializeComponent();         this.EventLog.Source = "BatchProcessorService";//This line causes warning }


All Replies

  • Tuesday, August 07, 2012 9:43 AM
    Moderator
     
     

    Hi ludanemec,

    Thank you for posting in the MSDN forum.

    CA2214 : Microsoft.Usage : 'Service.Service()' contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences:

    It seems that the constructor of an unsealed type calls a virtual method defined in its class.

    Like the MSDN document CA2214: Do not call overridable methods in constructors , to fix a violation of this rule, do not call a type's virtual methods from within the type's constructors.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, August 08, 2012 7:27 AM
     
     

    Hi Jack,

    thanks for your response.

    Your response confirms only my assumptions that error is either in Frameworks's ServiceBase class or in setting EventLog.Source in constructor. This brings these questions in my mind:

    1. Why has not Microsoft correct ServiceBase class?

    2. Is there any other place where should I set EventLog source?

    3. If code works fine, is it safe to suppress warning?

  • Wednesday, August 08, 2012 9:17 AM
    Moderator
     
     

    Hi ludanemec,

    Glad to receive your reply.

    The warning isn’t the error, when we use the code analysis tool to analyze our app. It generated the warning, since it goes against some default rules. And the code analysis will give you a safety coding style suggestion by the warning.

    If you want to create custom Event Log for windows service, maybe you could get some useful information from this blog “How to Create Custom Event Log for Windows Service” and this document “EventLog.Source Property

    Since this forum is to discuss the code analysis tool, if still no help, to help you resolve this issue, you would post this issue in the development forum like this thread. Thanks for your understanding.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

  • Friday, August 10, 2012 12:23 PM
     
     

    Hi Zhai,

    I accept that my problem is not caused by CA. By I still feel than not my questions are answered and I do not know where to set my EventLog.Source for service.

    I have created another thread for this.

  • Monday, August 13, 2012 1:49 AM
    Moderator
     
     

    Hi ludanemec,

    Glad to receive your reply.

    Since you want to create custom Event Log for windows service, so I’m afraid that it is not the code analysis tool issue, we are changing the issue type to General Discussion because you have posted this issue in the development forum. Maybe other community members who are familiar with the Event Log for windows service have good suggestions. Thanks for your understanding.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

  • Monday, August 13, 2012 7:39 AM
     
     

    Hi,

    problem is already solved in this thread.

    Thank you for your support.