public static string LoggingIdentifier(this IGrain grain) { var type = grain.GetType(); try { string temp = null; if (typeof(IGrainWithIntegerKey).IsAssignableFrom(type)) { return($"[{grain.GetPrimaryKeyLong()}] "); } else if (typeof(IGrainWithIntegerCompoundKey).IsAssignableFrom(type)) { var key = grain.GetPrimaryKeyLong(out temp); return($"[{key} {temp}] "); } else if (typeof(IGrainWithGuidKey).IsAssignableFrom(type)) { return($"[{grain.GetPrimaryKey()}] "); } else if (typeof(IGrainWithGuidCompoundKey).IsAssignableFrom(type)) { var key = grain.GetPrimaryKey(out temp); return($"[{key} {temp}] "); } else if (typeof(IGrainWithStringKey).IsAssignableFrom(type)) { return($"[{grain.GetPrimaryKeyString()}] "); } } catch { } // Don't throw for logging methods. return(string.Empty); }
private void PublishEvent(MethodInfo targetMethod, IGrain target, Exception ex) { var grainEvent = EventPublisher.CreateEvent(); if (target.GetPrimaryKeyString() != null) { grainEvent.GrainKeyString = target.GetPrimaryKeyString(); } else if (target.IsPrimaryKeyBasedOnLong()) { grainEvent.GrainKeyLong = target.GetPrimaryKeyLong(out var keyExt); grainEvent.GrainKeyExtention = keyExt; } else { grainEvent.GrainKeyGuid = target.GetPrimaryKey(out var keyExt); grainEvent.GrainKeyExtention = keyExt; } if (target is Grain grainTarget) { grainEvent.SiloAddress = grainTarget.RuntimeIdentity; } grainEvent.SiloDeploymentId = ConfigBuilder.ClusterConfiguration.Globals.DeploymentId; grainEvent.TargetType = targetMethod.DeclaringType?.FullName; grainEvent.TargetMethod = targetMethod.Name; grainEvent.Exception = ex; grainEvent.ErrCode = ex != null ? null : (int?)0; try { EventPublisher.TryPublish(grainEvent); } catch (Exception) { EventsDiscarded.Increment(); } }
private async Task <object> Interceptor(MethodInfo targetMethod, InvokeMethodRequest request, IGrain target, IGrainMethodInvoker invoker) { if (targetMethod == null) { throw new ArgumentNullException(nameof(targetMethod)); } var declaringNameSpace = targetMethod.DeclaringType?.Namespace; // Do not intercept Orleans grains or other grains which should not be included in statistics. if (targetMethod.DeclaringType.GetCustomAttribute <ExcludeGrainFromStatisticsAttribute>() != null || declaringNameSpace?.StartsWith("Orleans") == true) { return(await invoker.Invoke(target, request)); } RequestTimings.GetOrCreate(); // Ensure request timings is created here and not in the grain call. RequestTimings.Current.Request.Start(); Exception ex = null; try { return(await invoker.Invoke(target, request)); } catch (Exception e) { ex = e; throw; } finally { RequestTimings.Current.Request.Stop(); var grainEvent = EventPublisher.CreateEvent(); if (target.GetPrimaryKeyString() != null) { grainEvent.GrainKeyString = target.GetPrimaryKeyString(); } else if (target.IsPrimaryKeyBasedOnLong()) { grainEvent.GrainKeyLong = target.GetPrimaryKeyLong(out var keyExt); grainEvent.GrainKeyExtention = keyExt; } else { grainEvent.GrainKeyGuid = target.GetPrimaryKey(out var keyExt); grainEvent.GrainKeyExtention = keyExt; } if (target is Grain grainTarget) { grainEvent.SiloAddress = grainTarget.RuntimeIdentity; } grainEvent.SiloDeploymentId = ConfigBuilder.ClusterConfiguration.Globals.DeploymentId; grainEvent.TargetType = targetMethod.DeclaringType?.FullName; grainEvent.TargetMethod = targetMethod.Name; grainEvent.Exception = ex; grainEvent.ErrCode = ex != null ? null : (int?)0; try { EventPublisher.TryPublish(grainEvent); } catch (Exception) { EventsDiscarded.Increment(); } } }