microsoft / service-fabric

Service Fabric is a distributed systems platform for packaging, deploying, and managing stateless and stateful distributed applications and containers at large scale.
https://docs.microsoft.com/en-us/azure/service-fabric/
MIT License
3.03k stars 401 forks source link

Using ServiceEventSource and ActorEventSource classes inside a common library #632

Open BuddhaBuddy1 opened 5 years ago

BuddhaBuddy1 commented 5 years ago

This is a repost of https://github.com/Azure/service-fabric-issues/issues/473, which was closed because the original poster was away and didn't respond until after it was closed. However, I am having the same issue. I want to abstract out ServiceEventSource to a base class in a library, but when I do I get a System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' on the ServiceRegistered call. It doesn't matter if it is in a library or a separate class in the same namespace.

StatefullService1.ServiceEventSource.cs

    [EventSource(Name = "StatefulService1")]
    internal sealed class ServiceEventSource : BaseServiceEventSource
    {
        public static readonly ServiceEventSource Current = new ServiceEventSource();

        static ServiceEventSource()
        {
            // A workaround for the problem where ETW activities do not get tracked until Tasks infrastructure is initialized.
            // This problem will be fixed in .NET Framework 4.6.2.
            Task.Run(() => { });
        }

        // Instance constructor is private to enforce singleton semantics
        private ServiceEventSource() : base() { }
    }

SharedLibrary.BaseServiceEventSource.cs

    public abstract class BaseServiceEventSource : EventSource
    {
         #region Keywords
         ...
         #endregion 

         #region Events
         ...
         #endregion 

         #region Private methods
         ...
         #endregion 
    }
BuddhaBuddy1 commented 5 years ago

@ravipal , you said in the original post that it worked for you. Can you post your code, and the version of the Service Fabric SDK you used?

ashishnegi commented 5 years ago

@BuddhaBuddy1 Can you catch the exception and post the stack trace here ? This would be a general eventsource issue and not service fabric specific. Right ?

rogierverkaik commented 3 years ago

I was facing the same problem and saw this issue here. This is the stack trace of the exception:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Diagnostics.Tracing.EventSource.WriteEventWithRelatedActivityIdCore(Int32 eventId, Guid* relatedActivityId, Int32 eventDataCount, EventData* data)
   at System.Diagnostics.Tracing.EventSource.WriteEvent(Int32 eventId, Int32 arg1, String arg2)
   at SharedLibrary.BaseServiceEventSource.ServiceTypeRegistered(Int32 hostProcessId, String serviceType) in C:\Users\Rogier\source\repos\ServiceFabricCommonLibrary\SharedLibrary\BaseServiceEventSource.cs:line 117
   at Service2.Program.Main() in C:\Users\Rogier\source\repos\ServiceFabricCommonLibrary\Service2\Program.cs:line 27

And the issue can be easily recreated by the example in the introduction post.

bloomk commented 3 years ago

Anything? I am also having this same issue..

tuhland commented 3 years ago

I think this is less an SF and more an ETW weirdness issue.

We've "solved" this by not using a base class. Instead we put one generic event source (LogDebug, LogError etc.) including the ETW attributes into the library. Since service type names etc. are logged into the events, you can then query by those to differentiate the output by service.