diff --git a/src/MassTransit.Audit/AuditSpecification.cs b/src/MassTransit.Audit/AuditSpecification.cs deleted file mode 100644 index 54918b2483..0000000000 --- a/src/MassTransit.Audit/AuditSpecification.cs +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the -// specific language governing permissions and limitations under the License. -namespace MassTransit.Audit -{ - using System; - using System.Linq; - using Util; - using Util.Scanning; - - - public class AuditSpecification : IMessageFilterConfigurator - { - readonly CompositeFilter _messageFilter; - - public AuditSpecification() - { - _messageFilter = new CompositeFilter(); - Filter = message => _messageFilter.Matches(message); - } - - public Func Filter { get; } - - void IMessageFilterConfigurator.Handle(params Type[] messageTypes) => - _messageFilter.Includes += message => Match(message, messageTypes); - - void IMessageFilterConfigurator.Handle() => - _messageFilter.Includes += message => Match(message, typeof(T)); - - void IMessageFilterConfigurator.Handle(Func filter) => - _messageFilter.Includes += message => Match(message, filter); - - void IMessageFilterConfigurator.Ignore(params Type[] messageTypes) => - _messageFilter.Excludes += message => Match(message, messageTypes); - - void IMessageFilterConfigurator.Ignore() => - _messageFilter.Excludes += message => Match(message, typeof(T)); - - void IMessageFilterConfigurator.Ignore(Func filter) => - _messageFilter.Excludes += message => Match(message, filter); - - static bool Match(object message, params Type[] messageTypes) - { - var types = TypeMetadataCache.GetMessageTypes(message.GetType()); - return types.Intersect(messageTypes).Any(); - } - - static bool Match(object message, Func filter) - where T : class - { - var messageOfT = message as T; - return messageOfT != null && filter(messageOfT); - } - } -} \ No newline at end of file diff --git a/src/MassTransit.Audit/BusExtensions.cs b/src/MassTransit.Audit/BusExtensions.cs deleted file mode 100644 index 22779af378..0000000000 --- a/src/MassTransit.Audit/BusExtensions.cs +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the -// specific language governing permissions and limitations under the License. -namespace MassTransit.Audit -{ - using System; - using MassTransit; - using Pipeline; - - - public static class BusExtensions - { - /// - /// Add audit for mesages that are sent or published. - /// - /// The Bus - /// Audit store - /// Filter configuration delegate - /// Message metadata factory. If opited, the default one will be used. - public static void UseSendAudit(this IBus bus, MessageAuditStore store, - Action configureFilter = null, - MessageAuditSendObserver.MetadataFactory metadataFactory = null) - { - var factory = metadataFactory ?? AuditMetadataFactories.DefaultSendContextMetadataFactory; - bus.ConnectSendObserver(new MessageAuditSendObserver(store, factory, configureFilter)); - bus.ConnectPublishObserver(new MessageAuditPublishObserver(store, factory, configureFilter)); - } - - /// - /// Add audit for mesages that are consumed. - /// - /// The Bus - /// Audit store - /// Filter configuration delegate - /// Message metadata factory. If opited, the default one will be used. - public static void UseConsumeAudit(this IConsumeObserverConnector bus, MessageAuditStore store, - Action configureFilter = null, - MessageAuditConsumeObserver.MetadataFactory metadataFactory = null) - { - bus.ConnectConsumeObserver( - new MessageAuditConsumeObserver(store, - metadataFactory ?? AuditMetadataFactories.DefaultConsumeContextMetadataFactory, - configureFilter)); - } - } -} \ No newline at end of file diff --git a/src/MassTransit.Audit/DefaultAuditMetadataProducer.cs b/src/MassTransit.Audit/DefaultAuditMetadataProducer.cs deleted file mode 100644 index 01a5e110ed..0000000000 --- a/src/MassTransit.Audit/DefaultAuditMetadataProducer.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the -// specific language governing permissions and limitations under the License. -namespace MassTransit.Audit -{ - using System.Linq; - - - public static class AuditMetadataFactories - { - public static readonly MessageAuditConsumeObserver.MetadataFactory DefaultConsumeContextMetadataFactory = - c => new MessageAuditMetadata - { - ContextType = "Consume", - ConversationId = c.ConversationId, - CorrelationId = c.CorrelationId, - InitiatorId = c.InitiatorId, - MessageId = c.MessageId, - RequestId = c.RequestId, - DestinationAddress = c.DestinationAddress?.AbsoluteUri, - SourceAddress = c.SourceAddress?.AbsoluteUri, - FaultAddress = c.FaultAddress?.AbsoluteUri, - ResponseAddress = c.ResponseAddress?.AbsoluteUri, - Headers = c.Headers?.GetAll()?.ToDictionary(k => k.Key, v => v.Value.ToString()) - }; - - public static readonly MessageAuditSendObserver.MetadataFactory DefaultSendContextMetadataFactory = - c => new MessageAuditMetadata - { - ContextType = "Send", - ConversationId = c.ConversationId, - CorrelationId = c.CorrelationId, - InitiatorId = c.InitiatorId, - MessageId = c.MessageId, - RequestId = c.RequestId, - DestinationAddress = c.DestinationAddress?.AbsoluteUri, - SourceAddress = c.SourceAddress?.AbsoluteUri, - FaultAddress = c.FaultAddress?.AbsoluteUri, - ResponseAddress = c.ResponseAddress?.AbsoluteUri, - Headers = c.Headers?.GetAll()?.ToDictionary(k => k.Key, v => v.Value.ToString()) - }; - } -} \ No newline at end of file diff --git a/src/MassTransit.Audit/IMessageFilterConfigurator.cs b/src/MassTransit.Audit/IMessageFilterConfigurator.cs deleted file mode 100644 index 435c0ab552..0000000000 --- a/src/MassTransit.Audit/IMessageFilterConfigurator.cs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the -// specific language governing permissions and limitations under the License. -namespace MassTransit.Audit -{ - using System; - - - public interface IMessageFilterConfigurator - { - void Handle(params Type[] messageTypes); - - void Handle() where T : class; - - void Handle(Func filter) where T : class; - - void Ignore(params Type[] messageTypes); - - void Ignore() where T : class; - - void Ignore(Func filter) where T : class; - } -} \ No newline at end of file diff --git a/src/MassTransit.Audit/MassTransit.Audit.csproj b/src/MassTransit.Audit/MassTransit.Audit.csproj deleted file mode 100644 index 4e0dcc1744..0000000000 --- a/src/MassTransit.Audit/MassTransit.Audit.csproj +++ /dev/null @@ -1,835 +0,0 @@ - - - - - Debug - AnyCPU - {AE85151C-C049-47B2-BD8B-9915A3103299} - Library - Properties - MassTransit.Audit - MassTransit.Audit - v4.5.2 - 512 - - - true - full - false - ..\..\bin\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - ..\..\bin\ - TRACE - prompt - 4 - - - - ..\..\packages\EntityFramework\lib\net45\EntityFramework.dll - - - ..\packages\GreenPipes.1.0.9\lib\net452\GreenPipes.dll - - - ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll - - - - - - - - - - - - - Properties\SolutionVersion.cs - - - - - - - - - - - - - - {6EFD69FC-CBCC-4F85-AEE0-EFBA73F4D273} - MassTransit - - - - - - - - - - - - ..\..\packages\GreenPipes\lib\net452\GreenPipes.dll - True - True - - - - - - - - - ..\..\packages\MassTransit\lib\net452\MassTransit.dll - True - True - - - - - - - - - ..\..\packages\NewId\lib\net452\NewId.dll - True - True - - - - - - - - - ..\..\packages\Newtonsoft.Json\lib\net20\Newtonsoft.Json.dll - True - True - - - - - - - ..\..\packages\Newtonsoft.Json\lib\net35\Newtonsoft.Json.dll - True - True - - - - - - - ..\..\packages\Newtonsoft.Json\lib\net40\Newtonsoft.Json.dll - True - True - - - - - - - - ..\..\packages\Newtonsoft.Json\lib\netstandard1.0\Newtonsoft.Json.dll - True - True - - - - - - - ..\..\packages\Newtonsoft.Json\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll - True - True - - - - - - - ..\..\packages\Newtonsoft.Json\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll - True - True - - - - - - - - - ..\..\packages\System.Collections\ref\netstandard1.0\System.Collections.dll - False - True - - - - - - - ..\..\packages\System.Collections\ref\netstandard1.3\System.Collections.dll - False - True - - - - - - - - - ..\..\packages\System.Diagnostics.Debug\ref\netstandard1.0\System.Diagnostics.Debug.dll - False - True - - - - - - - ..\..\packages\System.Diagnostics.Debug\ref\netstandard1.3\System.Diagnostics.Debug.dll - False - True - - - - - - - - - ..\..\packages\System.Dynamic.Runtime\lib\netstandard1.3\System.Dynamic.Runtime.dll - True - True - - - - - - - - - ..\..\packages\System.Globalization\ref\netstandard1.0\System.Globalization.dll - False - True - - - - - - - ..\..\packages\System.Globalization\ref\netstandard1.3\System.Globalization.dll - False - True - - - - - - - - - True - - - ..\..\packages\System.IO\lib\net462\System.IO.dll - True - True - - - - - - - ..\..\packages\System.IO\ref\netstandard1.0\System.IO.dll - False - True - - - - - - - ..\..\packages\System.IO\ref\netstandard1.3\System.IO.dll - False - True - - - - - - - ..\..\packages\System.IO\ref\netstandard1.5\System.IO.dll - False - True - - - - - - - - - True - - - - - - - - - ..\..\packages\System.IO.FileSystem\lib\net46\System.IO.FileSystem.dll - True - True - - - - - - - ..\..\packages\System.IO.FileSystem\ref\netstandard1.3\System.IO.FileSystem.dll - False - True - - - - - - - - - ..\..\packages\System.IO.FileSystem.Primitives\lib\net46\System.IO.FileSystem.Primitives.dll - True - True - - - - - - - ..\..\packages\System.IO.FileSystem.Primitives\lib\netstandard1.3\System.IO.FileSystem.Primitives.dll - True - True - - - - - - - - - ..\..\packages\System.Linq\lib\net463\System.Linq.dll - True - True - - - - - - - ..\..\packages\System.Linq\lib\netstandard1.6\System.Linq.dll - True - True - - - - - - - - - ..\..\packages\System.Linq.Expressions\lib\net463\System.Linq.Expressions.dll - True - True - - - - - - - ..\..\packages\System.Linq.Expressions\ref\netstandard1.0\System.Linq.Expressions.dll - False - True - - - - - - - ..\..\packages\System.Linq.Expressions\ref\netstandard1.3\System.Linq.Expressions.dll - False - True - - - - - - - ..\..\packages\System.Linq.Expressions\lib\netstandard1.6\System.Linq.Expressions.dll - True - True - - - - - - - - - ..\..\packages\System.ObjectModel\lib\netstandard1.3\System.ObjectModel.dll - True - True - - - - - - - - - ..\..\packages\System.Reflection\lib\net462\System.Reflection.dll - True - True - - - - - - - ..\..\packages\System.Reflection\ref\netstandard1.0\System.Reflection.dll - False - True - - - - - - - ..\..\packages\System.Reflection\ref\netstandard1.3\System.Reflection.dll - False - True - - - - - - - ..\..\packages\System.Reflection\ref\netstandard1.5\System.Reflection.dll - False - True - - - - - - - - - ..\..\packages\System.Reflection.Emit\lib\netstandard1.3\System.Reflection.Emit.dll - True - True - - - - - - - - - ..\..\packages\System.Reflection.Emit.ILGeneration\lib\netstandard1.3\System.Reflection.Emit.ILGeneration.dll - True - True - - - - - - - - - ..\..\packages\System.Reflection.Emit.Lightweight\lib\netstandard1.3\System.Reflection.Emit.Lightweight.dll - True - True - - - - - - - - - ..\..\packages\System.Reflection.TypeExtensions\lib\net46\System.Reflection.TypeExtensions.dll - True - True - - - - - - - ..\..\packages\System.Reflection.TypeExtensions\lib\net462\System.Reflection.TypeExtensions.dll - True - True - - - - - - - ..\..\packages\System.Reflection.TypeExtensions\ref\netstandard1.3\System.Reflection.TypeExtensions.dll - False - True - - - - - - - ..\..\packages\System.Reflection.TypeExtensions\lib\netstandard1.5\System.Reflection.TypeExtensions.dll - True - True - - - - - - - - - True - - - - - - - True - - - ..\..\packages\System.Runtime\lib\net462\System.Runtime.dll - True - True - - - - - - - ..\..\packages\System.Runtime\ref\netstandard1.0\System.Runtime.dll - False - True - - - - - - - ..\..\packages\System.Runtime\ref\netstandard1.2\System.Runtime.dll - False - True - - - - - - - ..\..\packages\System.Runtime\ref\netstandard1.3\System.Runtime.dll - False - True - - - - - - - ..\..\packages\System.Runtime\ref\netstandard1.5\System.Runtime.dll - False - True - - - - - - - - - ..\..\packages\System.Runtime.Extensions\lib\net462\System.Runtime.Extensions.dll - True - True - - - - - - - ..\..\packages\System.Runtime.Extensions\ref\netstandard1.0\System.Runtime.Extensions.dll - False - True - - - - - - - ..\..\packages\System.Runtime.Extensions\ref\netstandard1.3\System.Runtime.Extensions.dll - False - True - - - - - - - ..\..\packages\System.Runtime.Extensions\ref\netstandard1.5\System.Runtime.Extensions.dll - False - True - - - - - - - - - ..\..\packages\System.Runtime.Handles\ref\netstandard1.3\System.Runtime.Handles.dll - False - True - - - - - - - - - ..\..\packages\System.Runtime.InteropServices\lib\net462\System.Runtime.InteropServices.dll - True - True - - - - - - - ..\..\packages\System.Runtime.InteropServices\lib\net463\System.Runtime.InteropServices.dll - True - True - - - - - - - ..\..\packages\System.Runtime.InteropServices\ref\netstandard1.1\System.Runtime.InteropServices.dll - False - True - - - - - - - ..\..\packages\System.Runtime.InteropServices\ref\netstandard1.2\System.Runtime.InteropServices.dll - False - True - - - - - - - ..\..\packages\System.Runtime.InteropServices\ref\netstandard1.3\System.Runtime.InteropServices.dll - False - True - - - - - - - ..\..\packages\System.Runtime.InteropServices\ref\netstandard1.5\System.Runtime.InteropServices.dll - False - True - - - - - - - - - ..\..\packages\System.Runtime.Serialization.Primitives\lib\netstandard1.3\System.Runtime.Serialization.Primitives.dll - True - True - - - - - - - - - ..\..\packages\System.Text.Encoding\ref\netstandard1.0\System.Text.Encoding.dll - False - True - - - - - - - ..\..\packages\System.Text.Encoding\ref\netstandard1.3\System.Text.Encoding.dll - False - True - - - - - - - - - ..\..\packages\System.Text.Encoding.Extensions\ref\netstandard1.0\System.Text.Encoding.Extensions.dll - False - True - - - - - - - ..\..\packages\System.Text.Encoding.Extensions\ref\netstandard1.3\System.Text.Encoding.Extensions.dll - False - True - - - - - - - - - ..\..\packages\System.Text.RegularExpressions\lib\net463\System.Text.RegularExpressions.dll - True - True - - - - - - - ..\..\packages\System.Text.RegularExpressions\ref\netstandard1.0\System.Text.RegularExpressions.dll - False - True - - - - - - - ..\..\packages\System.Text.RegularExpressions\ref\netstandard1.3\System.Text.RegularExpressions.dll - False - True - - - - - - - ..\..\packages\System.Text.RegularExpressions\lib\netstandard1.6\System.Text.RegularExpressions.dll - True - True - - - - - - - - - ..\..\packages\System.Threading\lib\netstandard1.3\System.Threading.dll - True - True - - - - - - - - - ..\..\packages\System.Threading.Tasks\ref\netstandard1.0\System.Threading.Tasks.dll - False - True - - - - - - - ..\..\packages\System.Threading.Tasks\ref\netstandard1.3\System.Threading.Tasks.dll - False - True - - - - - - - - - ..\..\packages\System.Threading.Tasks.Extensions\lib\netstandard1.0\System.Threading.Tasks.Extensions.dll - True - True - - - - - - - - - ..\..\packages\System.Xml.ReaderWriter\lib\net46\System.Xml.ReaderWriter.dll - True - True - - - - - - - ..\..\packages\System.Xml.ReaderWriter\lib\netstandard1.3\System.Xml.ReaderWriter.dll - True - True - - - - - - - - - ..\..\packages\System.Xml.XDocument\lib\netstandard1.3\System.Xml.XDocument.dll - True - True - - - - - \ No newline at end of file diff --git a/src/MassTransit.Audit/MessageAuditConsumeObserver.cs b/src/MassTransit.Audit/MessageAuditConsumeObserver.cs deleted file mode 100644 index ac5a4604ae..0000000000 --- a/src/MassTransit.Audit/MessageAuditConsumeObserver.cs +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the -// specific language governing permissions and limitations under the License. -namespace MassTransit.Audit -{ - using System; - using System.Threading.Tasks; - using MassTransit; - - - public class MessageAuditConsumeObserver : IConsumeObserver - { - public delegate MessageAuditMetadata MetadataFactory(ConsumeContext context); - - readonly MessageAuditStore _store; - readonly MetadataFactory _metaDataFactory; - readonly AuditSpecification _specification; - - public MessageAuditConsumeObserver(MessageAuditStore store, - MetadataFactory metaDataFactory, - Action configure) - { - _store = store; - _metaDataFactory = metaDataFactory; - _specification = new AuditSpecification(); - configure?.Invoke(_specification); - } - - public async Task PreConsume(ConsumeContext context) where T : class - { - if (!_specification.Filter(context.Message)) - return; - - await _store.StoreMessage( - context.Message, - context.Message.GetType().FullName, - _metaDataFactory(context)); - } - - public Task PostConsume(ConsumeContext context) where T : class - => Task.FromResult(0); - - public Task ConsumeFault(ConsumeContext context, Exception exception) where T : class - => Task.FromResult(0); - } -} \ No newline at end of file diff --git a/src/MassTransit.Audit/MessageAuditPublishObserver.cs b/src/MassTransit.Audit/MessageAuditPublishObserver.cs deleted file mode 100644 index 3c1cafec86..0000000000 --- a/src/MassTransit.Audit/MessageAuditPublishObserver.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the -// specific language governing permissions and limitations under the License. -namespace MassTransit.Audit -{ - using System; - using System.Threading.Tasks; - using MassTransit; - - - public class MessageAuditPublishObserver : IPublishObserver - { - readonly MessageAuditStore _store; - readonly MessageAuditSendObserver.MetadataFactory _metaDataFactory; - readonly AuditSpecification _specification; - - public MessageAuditPublishObserver(MessageAuditStore store, - MessageAuditSendObserver.MetadataFactory metaDataFactory, - Action configure) - { - _store = store; - _metaDataFactory = metaDataFactory; - _specification = new AuditSpecification(); - configure?.Invoke(_specification); - } - - public Task PrePublish(PublishContext context) where T : class => Task.FromResult(0); - - public async Task PostPublish(PublishContext context) where T : class - { - if (!_specification.Filter(context.Message)) - return; - - await _store.StoreMessage( - context.Message, - context.Message.GetType().FullName, - _metaDataFactory(context)); - } - - public Task PublishFault(PublishContext context, Exception exception) where T : class - => Task.FromResult(0); - } -} \ No newline at end of file diff --git a/src/MassTransit.Audit/MessageAuditSendObserver.cs b/src/MassTransit.Audit/MessageAuditSendObserver.cs deleted file mode 100644 index 45d74aaea6..0000000000 --- a/src/MassTransit.Audit/MessageAuditSendObserver.cs +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the -// specific language governing permissions and limitations under the License. -namespace MassTransit.Audit -{ - using System; - using System.Threading.Tasks; - using MassTransit; - - - public class MessageAuditSendObserver : ISendObserver - { - public delegate MessageAuditMetadata MetadataFactory(SendContext context); - - readonly MessageAuditStore _store; - readonly MetadataFactory _metaDataFactory; - readonly AuditSpecification _specification; - - public MessageAuditSendObserver(MessageAuditStore store, - MetadataFactory metaDataFactory, - Action configure) - { - _store = store; - _metaDataFactory = metaDataFactory; - _specification = new AuditSpecification(); - configure?.Invoke(_specification); - } - - public Task PreSend(SendContext context) where T : class - => Task.FromResult(0); - - public async Task PostSend(SendContext context) where T : class - { - if (!_specification.Filter(context.Message)) - return; - - await _store.StoreMessage( - context.Message, - context.Message.GetType().FullName, - _metaDataFactory(context)); - } - - public Task SendFault(SendContext context, Exception exception) where T : class - => Task.FromResult(0); - } -} \ No newline at end of file diff --git a/src/MassTransit.Audit/packages.config b/src/MassTransit.Audit/packages.config deleted file mode 100644 index 45f9291f26..0000000000 --- a/src/MassTransit.Audit/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/src/MassTransit.Tests/Audit/AuditFilter_Specs.cs b/src/MassTransit.Tests/Audit/AuditFilter_Specs.cs index fc5171ee82..749ed1528e 100644 --- a/src/MassTransit.Tests/Audit/AuditFilter_Specs.cs +++ b/src/MassTransit.Tests/Audit/AuditFilter_Specs.cs @@ -14,6 +14,7 @@ namespace MassTransit.Audit.Tests { using System.Linq; using System.Threading.Tasks; + using MassTransit.Tests.Audit; using NUnit.Framework; using Shouldly; using Testing; @@ -32,30 +33,36 @@ public async Task Send_message_to_test_consumer() _harness = new InMemoryTestHarness(); _harness.OnConnectObservers += bus => { - bus.UseSendAudit(_store, c => c.Ignore()); - bus.UseConsumeAudit(_store, c => c.Ignore()); + bus.ConnectSendAuditObservers(_store, c => c.Exclude()); + bus.ConnectConsumeAuditObserver(_store, c => c.Exclude()); }; _harness.Consumer(); await _harness.Start(); - await _harness.InputQueueSendEndpoint.Send(new A()); + var response = _harness.SubscribeHandler(); + + await _harness.InputQueueSendEndpoint.Send(new A(), x => x.ResponseAddress = _harness.BusAddress); + + await response; } [Test] public async Task Should_audit_and_filter_sent_messages() { - var sentCount = _harness.Sent.Select().Count(); - _store.Audit.Count(x => x.Metadata.ContextType == "Send").ShouldBe(sentCount); - _store.Audit.Select(x => x.ShouldBeOfType()); + var expected = _harness.Sent.Select().Any(); + var expectedB = _harness.Sent.Select().Any(); + + _store.Count(x => x.Metadata.ContextType == "Send").ShouldBe(1); } [Test] public async Task Should_audit_and_filter_consumed_messages() { - var consumedCount = _harness.Consumed.Select().Count(); - _store.Audit.Count(x => x.Metadata.ContextType == "Consume").ShouldBe(consumedCount); - _store.Audit.Select(x => x.ShouldBeOfType()); + bool expected = _harness.Consumed.Select().Any(); + bool expectedB = _harness.Consumed.Select().Any(); + + _store.Count(x => x.Metadata.ContextType == "Consume").ShouldBe(1); } diff --git a/src/MassTransit.Tests/Audit/Audit_Specs.cs b/src/MassTransit.Tests/Audit/Audit_Specs.cs index 71d4ec671d..323a28e189 100644 --- a/src/MassTransit.Tests/Audit/Audit_Specs.cs +++ b/src/MassTransit.Tests/Audit/Audit_Specs.cs @@ -14,6 +14,7 @@ namespace MassTransit.Audit.Tests { using System.Linq; using System.Threading.Tasks; + using MassTransit.Tests.Audit; using NUnit.Framework; using Shouldly; using Testing; @@ -32,28 +33,34 @@ public async Task Send_message_to_test_consumer() _harness = new InMemoryTestHarness(); _harness.OnConnectObservers += bus => { - bus.UseSendAudit(_store); - bus.UseConsumeAudit(_store); + bus.ConnectSendAuditObservers(_store); + bus.ConnectConsumeAuditObserver(_store); }; _harness.Consumer(); await _harness.Start(); - await _harness.InputQueueSendEndpoint.Send(new A()); + var response = _harness.SubscribeHandler(); + + await _harness.InputQueueSendEndpoint.Send(new A(), x => x.ResponseAddress = _harness.BusAddress); + + await response; } [Test] public async Task Should_audit_sent_messages() { - var sentCount = _harness.Sent.Select().Count(); - _store.Audit.Count(x => x.Metadata.ContextType == "Send").ShouldBe(sentCount); + var expected = _harness.Sent.Select().Any(); + var expectedB = _harness.Sent.Select().Any(); + _store.Count(x => x.Metadata.ContextType == "Send").ShouldBe(2); } [Test] public async Task Should_audit_consumed_messages() { - var consumedCount = _harness.Consumed.Select().Count(); - _store.Audit.Count(x => x.Metadata.ContextType == "Consume").ShouldBe(consumedCount); + bool expected = _harness.Consumed.Select().Any(); + bool expectedB = _harness.Consumed.Select().Any(); + _store.Count(x => x.Metadata.ContextType == "Consume").ShouldBe(2); } diff --git a/src/MassTransit.Tests/Audit/InMemoryAuditStore.cs b/src/MassTransit.Tests/Audit/InMemoryAuditStore.cs index 366c70fdf6..85cfcd5896 100644 --- a/src/MassTransit.Tests/Audit/InMemoryAuditStore.cs +++ b/src/MassTransit.Tests/Audit/InMemoryAuditStore.cs @@ -1,46 +1,79 @@ // Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// // Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. -namespace MassTransit.Audit.Tests +namespace MassTransit.Tests.Audit { + using System; + using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; - using GreenPipes.Util; + using MassTransit.Audit; + using Util; + using Util.Caching; - public class InMemoryAuditStore : MessageAuditStore + public class InMemoryAuditStore : + IMessageAuditStore, + IEnumerable { - internal List Audit = new List(); + readonly ICache _audits; + readonly IIndex _messageId; - public Task StoreMessage(object message, string messageType, MessageAuditMetadata metadata) + public InMemoryAuditStore() { - Audit.Add(new AuditRecord(message, messageType, metadata)); + _audits = new GreenCache(10000, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(60), () => DateTime.UtcNow); + _messageId = _audits.AddIndex("messageId", x => x.Metadata.MessageId.Value); + } + + Task IMessageAuditStore.StoreMessage(T message, MessageAuditMetadata metadata) + { + _audits.Add(new AuditRecord(message, metadata)); + return TaskUtil.Completed; } - internal class AuditRecord + public interface AuditRecord + { + string MessageType { get; } + MessageAuditMetadata Metadata { get; } + } + + + public class AuditRecord : + AuditRecord + where T : class { - public AuditRecord(object message, string messageType, MessageAuditMetadata metadata) + public AuditRecord(T message, MessageAuditMetadata metadata) { Message = message; - MessageType = messageType; + MessageType = TypeMetadataCache.ShortName; Metadata = metadata; } - public object Message { get; } + public T Message { get; } public string MessageType { get; } public MessageAuditMetadata Metadata { get; } } - } + + public IEnumerator GetEnumerator() + { + return _audits.GetAll().GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } } \ No newline at end of file diff --git a/src/MassTransit.sln b/src/MassTransit.sln index f785a7ee70..a677854f3e 100644 --- a/src/MassTransit.sln +++ b/src/MassTransit.sln @@ -1,4 +1,5 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 + +Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 @@ -116,8 +117,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MassTransit.MartenIntegrati EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MassTransit.MartenIntegration.Tests", "Persistence\MassTransit.MartenIntegration.Tests\MassTransit.MartenIntegration.Tests.csproj", "{92BE64BB-FF25-4CC8-B64F-3BF02F18F089}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MassTransit.Audit", "MassTransit.Audit\MassTransit.Audit.csproj", "{AE85151C-C049-47B2-BD8B-9915A3103299}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -483,6 +482,30 @@ Global {E390AD5C-61D3-4EF4-9858-5DBE0F0849D9}.ReleaseUnsigned|Any CPU.ActiveCfg = Release|Any CPU {E390AD5C-61D3-4EF4-9858-5DBE0F0849D9}.ReleaseUnsigned|x86.ActiveCfg = Release|Any CPU {E390AD5C-61D3-4EF4-9858-5DBE0F0849D9}.ReleaseUnsigned|x86.Build.0 = Release|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Debug|x86.ActiveCfg = Debug|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Debug|x86.Build.0 = Debug|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Release|Any CPU.Build.0 = Release|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Release|x86.ActiveCfg = Release|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Release|x86.Build.0 = Release|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.ReleaseUnsigned|Any CPU.ActiveCfg = Release|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.ReleaseUnsigned|Any CPU.Build.0 = Release|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.ReleaseUnsigned|x86.ActiveCfg = Release|Any CPU + {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.ReleaseUnsigned|x86.Build.0 = Release|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.Debug|x86.ActiveCfg = Debug|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.Debug|x86.Build.0 = Debug|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.Release|Any CPU.Build.0 = Release|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.Release|x86.ActiveCfg = Release|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.Release|x86.Build.0 = Release|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.ReleaseUnsigned|Any CPU.ActiveCfg = Release|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.ReleaseUnsigned|Any CPU.Build.0 = Release|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.ReleaseUnsigned|x86.ActiveCfg = Release|Any CPU + {4A042032-21A2-40CA-BBE7-5D47F786A949}.ReleaseUnsigned|x86.Build.0 = Release|Any CPU {3016A502-4855-477F-9113-1A6B224BD9E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3016A502-4855-477F-9113-1A6B224BD9E0}.Debug|Any CPU.Build.0 = Debug|Any CPU {3016A502-4855-477F-9113-1A6B224BD9E0}.Debug|x86.ActiveCfg = Debug|Any CPU @@ -531,42 +554,6 @@ Global {92BE64BB-FF25-4CC8-B64F-3BF02F18F089}.ReleaseUnsigned|Any CPU.Build.0 = Release|Any CPU {92BE64BB-FF25-4CC8-B64F-3BF02F18F089}.ReleaseUnsigned|x86.ActiveCfg = Release|Any CPU {92BE64BB-FF25-4CC8-B64F-3BF02F18F089}.ReleaseUnsigned|x86.Build.0 = Release|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Debug|x86.ActiveCfg = Debug|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Debug|x86.Build.0 = Debug|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Release|Any CPU.Build.0 = Release|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Release|x86.ActiveCfg = Release|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.Release|x86.Build.0 = Release|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.ReleaseUnsigned|Any CPU.ActiveCfg = Release|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.ReleaseUnsigned|Any CPU.Build.0 = Release|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.ReleaseUnsigned|x86.ActiveCfg = Release|Any CPU - {B2C9F22A-459A-4828-8017-0DB76F81F1A1}.ReleaseUnsigned|x86.Build.0 = Release|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.Debug|x86.ActiveCfg = Debug|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.Debug|x86.Build.0 = Debug|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.Release|Any CPU.Build.0 = Release|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.Release|x86.ActiveCfg = Release|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.Release|x86.Build.0 = Release|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.ReleaseUnsigned|Any CPU.ActiveCfg = Release|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.ReleaseUnsigned|Any CPU.Build.0 = Release|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.ReleaseUnsigned|x86.ActiveCfg = Release|Any CPU - {4A042032-21A2-40CA-BBE7-5D47F786A949}.ReleaseUnsigned|x86.Build.0 = Release|Any CPU - {AE85151C-C049-47B2-BD8B-9915A3103299}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {AE85151C-C049-47B2-BD8B-9915A3103299}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AE85151C-C049-47B2-BD8B-9915A3103299}.Debug|x86.ActiveCfg = Debug|x86 - {AE85151C-C049-47B2-BD8B-9915A3103299}.Debug|x86.Build.0 = Debug|x86 - {AE85151C-C049-47B2-BD8B-9915A3103299}.Release|Any CPU.ActiveCfg = Release|Any CPU - {AE85151C-C049-47B2-BD8B-9915A3103299}.Release|Any CPU.Build.0 = Release|Any CPU - {AE85151C-C049-47B2-BD8B-9915A3103299}.Release|x86.ActiveCfg = Release|x86 - {AE85151C-C049-47B2-BD8B-9915A3103299}.Release|x86.Build.0 = Release|x86 - {AE85151C-C049-47B2-BD8B-9915A3103299}.ReleaseUnsigned|Any CPU.ActiveCfg = ReleaseUnsigned|Any CPU - {AE85151C-C049-47B2-BD8B-9915A3103299}.ReleaseUnsigned|Any CPU.Build.0 = ReleaseUnsigned|Any CPU - {AE85151C-C049-47B2-BD8B-9915A3103299}.ReleaseUnsigned|x86.ActiveCfg = ReleaseUnsigned|x86 - {AE85151C-C049-47B2-BD8B-9915A3103299}.ReleaseUnsigned|x86.Build.0 = ReleaseUnsigned|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/MassTransit.Audit/MessageAuditStore.cs b/src/MassTransit/Audit/IConsumeMetadataFactory.cs similarity index 55% rename from src/MassTransit.Audit/MessageAuditStore.cs rename to src/MassTransit/Audit/IConsumeMetadataFactory.cs index edfc91c1db..897d83f16d 100644 --- a/src/MassTransit.Audit/MessageAuditStore.cs +++ b/src/MassTransit/Audit/IConsumeMetadataFactory.cs @@ -1,22 +1,20 @@ -// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the -// specific language governing permissions and limitations under the License. -namespace MassTransit.Audit -{ - using System.Threading.Tasks; - - - public interface MessageAuditStore - { - Task StoreMessage(object message, string messageType, MessageAuditMetadata metadata); - } +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Audit +{ + public interface IConsumeMetadataFactory + { + MessageAuditMetadata CreateAuditMetadata(ConsumeContext context) + where T : class; + } } \ No newline at end of file diff --git a/src/MassTransit/Audit/IMessageAuditStore.cs b/src/MassTransit/Audit/IMessageAuditStore.cs new file mode 100644 index 0000000000..ea9f04aabb --- /dev/null +++ b/src/MassTransit/Audit/IMessageAuditStore.cs @@ -0,0 +1,33 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Audit +{ + using System.Threading.Tasks; + + + /// + /// Used to store message audits that are observed + /// + public interface IMessageAuditStore + { + /// + /// Store the message audit, with associated metadata + /// + /// The message type + /// The message itself + /// The message metadata + /// + Task StoreMessage(T message, MessageAuditMetadata metadata) + where T : class; + } +} \ No newline at end of file diff --git a/src/MassTransit/Audit/ISendMetadataFactory.cs b/src/MassTransit/Audit/ISendMetadataFactory.cs new file mode 100644 index 0000000000..0fe5120e1b --- /dev/null +++ b/src/MassTransit/Audit/ISendMetadataFactory.cs @@ -0,0 +1,23 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Audit +{ + public interface ISendMetadataFactory + { + MessageAuditMetadata CreateAuditMetadata(SendContext context) + where T : class; + + MessageAuditMetadata CreateAuditMetadata(PublishContext context) + where T : class; + } +} \ No newline at end of file diff --git a/src/MassTransit.Audit/MessageAuditMetadata.cs b/src/MassTransit/Audit/MessageAuditMetadata.cs similarity index 100% rename from src/MassTransit.Audit/MessageAuditMetadata.cs rename to src/MassTransit/Audit/MessageAuditMetadata.cs diff --git a/src/MassTransit/Audit/MetadataFactories/DefaultConsumeMetadataFactory.cs b/src/MassTransit/Audit/MetadataFactories/DefaultConsumeMetadataFactory.cs new file mode 100644 index 0000000000..64c278f8ad --- /dev/null +++ b/src/MassTransit/Audit/MetadataFactories/DefaultConsumeMetadataFactory.cs @@ -0,0 +1,44 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Audit.MetadataFactories +{ + using System.Linq; + + + public class DefaultConsumeMetadataFactory : + IConsumeMetadataFactory + { + MessageAuditMetadata IConsumeMetadataFactory.CreateAuditMetadata(ConsumeContext context) + { + return CreateMetadata(context, "Consume"); + } + + static MessageAuditMetadata CreateMetadata(MessageContext context, string contextType) + { + return new MessageAuditMetadata + { + ContextType = contextType, + ConversationId = context.ConversationId, + CorrelationId = context.CorrelationId, + InitiatorId = context.InitiatorId, + MessageId = context.MessageId, + RequestId = context.RequestId, + DestinationAddress = context.DestinationAddress?.AbsoluteUri, + SourceAddress = context.SourceAddress?.AbsoluteUri, + FaultAddress = context.FaultAddress?.AbsoluteUri, + ResponseAddress = context.ResponseAddress?.AbsoluteUri, + Headers = context.Headers?.GetAll()?.ToDictionary(k => k.Key, v => v.Value.ToString()) + }; + } + } +} \ No newline at end of file diff --git a/src/MassTransit/Audit/MetadataFactories/DefaultSendMetadataFactory.cs b/src/MassTransit/Audit/MetadataFactories/DefaultSendMetadataFactory.cs new file mode 100644 index 0000000000..01015ab8cc --- /dev/null +++ b/src/MassTransit/Audit/MetadataFactories/DefaultSendMetadataFactory.cs @@ -0,0 +1,49 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Audit.MetadataFactories +{ + using System.Linq; + + + public class DefaultSendMetadataFactory : + ISendMetadataFactory + { + MessageAuditMetadata ISendMetadataFactory.CreateAuditMetadata(SendContext context) + { + return CreateMetadata(context, "Send"); + } + + MessageAuditMetadata ISendMetadataFactory.CreateAuditMetadata(PublishContext context) + { + return CreateMetadata(context, "Publish"); + } + + static MessageAuditMetadata CreateMetadata(SendContext context, string contextType) + { + return new MessageAuditMetadata + { + ContextType = contextType, + ConversationId = context.ConversationId, + CorrelationId = context.CorrelationId, + InitiatorId = context.InitiatorId, + MessageId = context.MessageId, + RequestId = context.RequestId, + DestinationAddress = context.DestinationAddress?.AbsoluteUri, + SourceAddress = context.SourceAddress?.AbsoluteUri, + FaultAddress = context.FaultAddress?.AbsoluteUri, + ResponseAddress = context.ResponseAddress?.AbsoluteUri, + Headers = context.Headers?.GetAll()?.ToDictionary(k => k.Key, v => v.Value.ToString()) + }; + } + } +} \ No newline at end of file diff --git a/src/MassTransit/Audit/Observers/AuditConsumeObserver.cs b/src/MassTransit/Audit/Observers/AuditConsumeObserver.cs new file mode 100644 index 0000000000..7e1043a8ed --- /dev/null +++ b/src/MassTransit/Audit/Observers/AuditConsumeObserver.cs @@ -0,0 +1,50 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Audit.Observers +{ + using System; + using System.Threading.Tasks; + using Util; + using Util.Scanning; + + + public class AuditConsumeObserver : + IConsumeObserver + { + readonly CompositeFilter _filter; + readonly IConsumeMetadataFactory _metadataFactory; + + readonly IMessageAuditStore _store; + + public AuditConsumeObserver(IMessageAuditStore store, IConsumeMetadataFactory metadataFactory, CompositeFilter filter) + { + _store = store; + _metadataFactory = metadataFactory; + _filter = filter; + } + + Task IConsumeObserver.PreConsume(ConsumeContext context) + { + if (!_filter.Matches(context)) + return TaskUtil.Completed; + + var metadata = _metadataFactory.CreateAuditMetadata(context); + + return _store.StoreMessage(context.Message, metadata); + } + + Task IConsumeObserver.PostConsume(ConsumeContext context) => TaskUtil.Completed; + + Task IConsumeObserver.ConsumeFault(ConsumeContext context, Exception exception) => TaskUtil.Completed; + } +} \ No newline at end of file diff --git a/src/MassTransit/Audit/Observers/AuditPublishObserver.cs b/src/MassTransit/Audit/Observers/AuditPublishObserver.cs new file mode 100644 index 0000000000..ad7adde4f7 --- /dev/null +++ b/src/MassTransit/Audit/Observers/AuditPublishObserver.cs @@ -0,0 +1,49 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Audit.Observers +{ + using System; + using System.Threading.Tasks; + using Util; + using Util.Scanning; + + + public class AuditPublishObserver : + IPublishObserver + { + readonly ISendMetadataFactory _metadataFactory; + readonly CompositeFilter _filter; + readonly IMessageAuditStore _store; + + public AuditPublishObserver(IMessageAuditStore store, ISendMetadataFactory metadataFactory, CompositeFilter filter) + { + _store = store; + _metadataFactory = metadataFactory; + _filter = filter; + } + + Task IPublishObserver.PrePublish(PublishContext context) => TaskUtil.Completed; + + Task IPublishObserver.PostPublish(PublishContext context) + { + if (!_filter.Matches(context)) + return TaskUtil.Completed; + + var metadata = _metadataFactory.CreateAuditMetadata(context); + + return _store.StoreMessage(context.Message, metadata); + } + + Task IPublishObserver.PublishFault(PublishContext context, Exception exception) => TaskUtil.Completed; + } +} \ No newline at end of file diff --git a/src/MassTransit/Audit/Observers/AuditSendObserver.cs b/src/MassTransit/Audit/Observers/AuditSendObserver.cs new file mode 100644 index 0000000000..a3d8c7e2ae --- /dev/null +++ b/src/MassTransit/Audit/Observers/AuditSendObserver.cs @@ -0,0 +1,49 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Audit.Observers +{ + using System; + using System.Threading.Tasks; + using Util; + using Util.Scanning; + + + public class AuditSendObserver : + ISendObserver + { + readonly ISendMetadataFactory _metadataFactory; + readonly CompositeFilter _filter; + readonly IMessageAuditStore _store; + + public AuditSendObserver(IMessageAuditStore store, ISendMetadataFactory metadataFactory, CompositeFilter filter) + { + _store = store; + _metadataFactory = metadataFactory; + _filter = filter; + } + + Task ISendObserver.PreSend(SendContext context) => TaskUtil.Completed; + + Task ISendObserver.PostSend(SendContext context) + { + if (!_filter.Matches(context)) + return TaskUtil.Completed; + + var metadata = _metadataFactory.CreateAuditMetadata(context); + + return _store.StoreMessage(context.Message, metadata); + } + + Task ISendObserver.SendFault(SendContext context, Exception exception) => TaskUtil.Completed; + } +} \ No newline at end of file diff --git a/src/MassTransit/Configuration/AuditConfigurationExtensions.cs b/src/MassTransit/Configuration/AuditConfigurationExtensions.cs new file mode 100644 index 0000000000..4573e83ce1 --- /dev/null +++ b/src/MassTransit/Configuration/AuditConfigurationExtensions.cs @@ -0,0 +1,68 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit +{ + using System; + using Audit; + using Audit.MetadataFactories; + using Audit.Observers; + using Configurators; + using Pipeline; + + + public static class AuditConfigurationExtensions + { + /// + /// Adds observers that will audit all published and sent messages, sending them to the message audit store after they are sent/published. + /// + /// The bus + /// Audit store + /// Filter configuration delegate + /// Message metadata factory. If omited, the default one will be used. + public static void ConnectSendAuditObservers(this T connector, IMessageAuditStore store, Action configureFilter = null, + ISendMetadataFactory metadataFactory = null) + where T : ISendObserverConnector, IPublishObserverConnector + { + var specification = new SendMessageFilterSpecification(); + configureFilter?.Invoke(specification); + + var factory = metadataFactory ?? new DefaultSendMetadataFactory(); + + connector.ConnectSendObserver(new AuditSendObserver(store, factory, specification.Filter)); + connector.ConnectPublishObserver(new AuditPublishObserver(store, factory, specification.Filter)); + } + + /// + /// Add an observer that will audit consumed messages, sending them to the message audit store prior to consumption by the consumer + /// + /// The bus or endpoint + /// The audit store + /// Filter configuration delegate + /// Message metadata factory. If omited, the default one will be used. + public static void ConnectConsumeAuditObserver(this IConsumeObserverConnector connector, IMessageAuditStore store, + Action configureFilter = null, IConsumeMetadataFactory metadataFactory = null) + { + if (connector == null) + throw new ArgumentNullException(nameof(connector)); + if (store == null) + throw new ArgumentNullException(nameof(store)); + + var specification = new ConsumeMessageFilterSpecification(); + configureFilter?.Invoke(specification); + + var factory = metadataFactory ?? new DefaultConsumeMetadataFactory(); + + connector.ConnectConsumeObserver(new AuditConsumeObserver(store, factory, specification.Filter)); + } + } +} \ No newline at end of file diff --git a/src/MassTransit/Configuration/Configurators/ConsumeMessageFilterSpecification.cs b/src/MassTransit/Configuration/Configurators/ConsumeMessageFilterSpecification.cs new file mode 100644 index 0000000000..47bc46c348 --- /dev/null +++ b/src/MassTransit/Configuration/Configurators/ConsumeMessageFilterSpecification.cs @@ -0,0 +1,62 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Configurators +{ + using System; + using System.Linq; + using Util.Scanning; + + + public class ConsumeMessageFilterSpecification : + IMessageFilterConfigurator + { + readonly CompositeFilter _filter; + + public ConsumeMessageFilterSpecification() + { + _filter = new CompositeFilter(); + } + + public CompositeFilter Filter => _filter; + + void IMessageFilterConfigurator.Include(params Type[] messageTypes) => + _filter.Includes += message => Match(message, messageTypes); + + void IMessageFilterConfigurator.Include() => + _filter.Includes += message => Match(message, typeof(T)); + + void IMessageFilterConfigurator.Include(Func filter) => + _filter.Includes += message => Match(message, filter); + + void IMessageFilterConfigurator.Exclude(params Type[] messageTypes) => + _filter.Excludes += message => Match(message, messageTypes); + + void IMessageFilterConfigurator.Exclude() => + _filter.Excludes += message => Match(message, typeof(T)); + + void IMessageFilterConfigurator.Exclude(Func filter) => + _filter.Excludes += message => Match(message, filter); + + static bool Match(ConsumeContext context, params Type[] messageTypes) + { + return messageTypes.Any(context.HasMessageType); + } + + static bool Match(ConsumeContext context, Func filter) + where T : class + { + ConsumeContext consumeContext; + return context.TryGetMessage(out consumeContext) && filter(consumeContext.Message); + } + } +} \ No newline at end of file diff --git a/src/MassTransit/Configuration/Configurators/SendMessageFilterSpecification.cs b/src/MassTransit/Configuration/Configurators/SendMessageFilterSpecification.cs new file mode 100644 index 0000000000..6cf2fb8895 --- /dev/null +++ b/src/MassTransit/Configuration/Configurators/SendMessageFilterSpecification.cs @@ -0,0 +1,63 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit.Configurators +{ + using System; + using System.Linq; + using Util.Scanning; + + + public class SendMessageFilterSpecification : + IMessageFilterConfigurator + { + readonly CompositeFilter _filter; + + public SendMessageFilterSpecification() + { + _filter = new CompositeFilter(); + } + + public CompositeFilter Filter => _filter; + + void IMessageFilterConfigurator.Include(params Type[] messageTypes) => + _filter.Includes += message => Match(message, messageTypes); + + void IMessageFilterConfigurator.Include() => + _filter.Includes += message => Match(message, typeof(T)); + + void IMessageFilterConfigurator.Include(Func filter) => + _filter.Includes += message => Match(message, filter); + + void IMessageFilterConfigurator.Exclude(params Type[] messageTypes) => + _filter.Excludes += message => Match(message, messageTypes); + + void IMessageFilterConfigurator.Exclude() => + _filter.Excludes += message => Match(message, typeof(T)); + + void IMessageFilterConfigurator.Exclude(Func filter) => + _filter.Excludes += message => Match(message, filter); + + static bool Match(SendContext context, params Type[] messageTypes) + { + return messageTypes.Any(x => typeof(SendContext<>).MakeGenericType(x).IsInstanceOfType(context)); + } + + static bool Match(SendContext context, Func filter) + where T : class + { + var sendContext = context as SendContext; + + return sendContext != null && filter(sendContext.Message); + } + } +} \ No newline at end of file diff --git a/src/MassTransit/Configuration/IMessageFilterConfigurator.cs b/src/MassTransit/Configuration/IMessageFilterConfigurator.cs new file mode 100644 index 0000000000..de3552698c --- /dev/null +++ b/src/MassTransit/Configuration/IMessageFilterConfigurator.cs @@ -0,0 +1,61 @@ +// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. +namespace MassTransit +{ + using System; + + + /// + /// Configures a message filter, for including and excluding message types + /// + public interface IMessageFilterConfigurator + { + /// + /// Include the message if it is any of the specified message types + /// + /// + void Include(params Type[] messageTypes); + + /// + /// Include the message if it is the specified message type + /// + /// The message type + void Include() where T : class; + + /// + /// Include the message if it is the specified message type and matches the specified filter expression + /// + /// The message type + /// The filter expression + void Include(Func filter) where T : class; + + /// + /// Exclude the message if it is any of the specified message types + /// + /// + void Exclude(params Type[] messageTypes); + + /// + /// Exclude the message if it is the specified message type + /// + /// The message type + void Exclude() where T : class; + + /// + /// Exclude the message if it is the specified message type and matches the specified filter expression + /// + /// The message type + /// The filter expression + void Exclude(Func filter) where T : class; + } +} \ No newline at end of file diff --git a/src/MassTransit/MassTransit.csproj b/src/MassTransit/MassTransit.csproj index 1db04caf39..7cc8c009dc 100644 --- a/src/MassTransit/MassTransit.csproj +++ b/src/MassTransit/MassTransit.csproj @@ -97,9 +97,22 @@ Properties\SolutionVersion.cs + + + + + + + + + + + + + diff --git a/src/MassTransit/Util/Caching/GreenCache.cs b/src/MassTransit/Util/Caching/GreenCache.cs index 98019ae69d..31ffb8f6b6 100644 --- a/src/MassTransit/Util/Caching/GreenCache.cs +++ b/src/MassTransit/Util/Caching/GreenCache.cs @@ -14,6 +14,7 @@ namespace MassTransit.Util.Caching { using System; using System.Collections.Generic; + using System.Linq; public class GreenCache : @@ -71,9 +72,9 @@ public void Clear() _nodeTracker.Clear(); } - public IEnumerable> GetAll() + public IEnumerable GetAll() { - return _nodeTracker.GetAll(); + return _nodeTracker.GetAll().Select(x => x.Value.Result); } } } \ No newline at end of file diff --git a/src/MassTransit/Util/Caching/ICache.cs b/src/MassTransit/Util/Caching/ICache.cs index d8a9693294..92b134e3f2 100644 --- a/src/MassTransit/Util/Caching/ICache.cs +++ b/src/MassTransit/Util/Caching/ICache.cs @@ -12,6 +12,9 @@ // specific language governing permissions and limitations under the License. namespace MassTransit.Util.Caching { + using System.Collections.Generic; + + public interface ICache where TValue : class { @@ -40,6 +43,12 @@ public interface ICache /// The value to add void Add(TValue value); + /// + /// Returns all the values in the cache + /// + /// + IEnumerable GetAll(); + /// /// Forcibly clear the cache immediately (disposal of cached items may take some time, occurs asynchronously) /// diff --git a/src/Persistence/MassTransit.EntityFrameworkIntegration.Tests/AuditStore_Specs.cs b/src/Persistence/MassTransit.EntityFrameworkIntegration.Tests/AuditStore_Specs.cs index d4bd8d1a83..14499adf8e 100644 --- a/src/Persistence/MassTransit.EntityFrameworkIntegration.Tests/AuditStore_Specs.cs +++ b/src/Persistence/MassTransit.EntityFrameworkIntegration.Tests/AuditStore_Specs.cs @@ -1,30 +1,51 @@ // Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// // Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. namespace MassTransit.EntityFrameworkIntegration.Tests { using System.Data.Entity; - using GreenPipes.Util; using System.Linq; using System.Threading.Tasks; using Audit; - using Testing; + using GreenPipes.Util; using NUnit.Framework; using Shouldly; + using Testing; [TestFixture] public class Saving_audit_records_to_the_audit_store { + [Test] + public async Task Should_have_consume_audit_records() + { + var consumed = _harness.Consumed; + await Task.Delay(500); + (await GetAuditRecords("Consume")).ShouldBe(consumed.Count()); + } + + [Test] + public async Task Should_have_send_audit_record() + { + var sent = _harness.Sent; + await Task.Delay(500); + (await GetAuditRecords("Send")).ShouldBe(sent.Count()); + } + + [SetUp] + public async Task CleanAudit() + { + } + InMemoryTestHarness _harness; ConsumerTestHarness _consumer; EntityFrameworkAuditStore _store; @@ -40,8 +61,8 @@ public async Task Send_message_to_test_consumer() _harness = new InMemoryTestHarness(); _harness.OnConnectObservers += bus => { - bus.UseSendAudit(_store); - bus.UseConsumeAudit(_store); + bus.ConnectSendAuditObservers(_store); + bus.ConnectConsumeAuditObserver(_store); }; _consumer = _harness.Consumer(); @@ -56,27 +77,6 @@ public async Task Teardown() await _harness.Stop(); } - [SetUp] - public async Task CleanAudit() - { - } - - [Test] - public async Task Should_have_send_audit_record() - { - var sent = _harness.Sent; - await Task.Delay(500); - (await GetAuditRecords("Send")).ShouldBe(sent.Count()); - } - - [Test] - public async Task Should_have_consume_audit_records() - { - var consumed = _harness.Consumed; - await Task.Delay(500); - (await GetAuditRecords("Consume")).ShouldBe(consumed.Count()); - } - async Task GetAuditRecords(string contextType) { using (var dbContext = _store.AuditContext) diff --git a/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/AuditMapping.cs b/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/AuditMapping.cs index 5afd32bdbe..35a87bc105 100644 --- a/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/AuditMapping.cs +++ b/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/AuditMapping.cs @@ -14,6 +14,7 @@ namespace MassTransit.EntityFrameworkIntegration { using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.ModelConfiguration; + using Audit; public class AuditMapping : EntityTypeConfiguration diff --git a/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/AuditRecord.cs b/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/AuditRecord.cs index d899216351..f41ed85055 100644 --- a/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/AuditRecord.cs +++ b/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/AuditRecord.cs @@ -1,21 +1,21 @@ // Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// // Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. -namespace MassTransit.EntityFrameworkIntegration +namespace MassTransit.EntityFrameworkIntegration.Audit { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; - using Audit; + using MassTransit.Audit; using Newtonsoft.Json; @@ -76,8 +76,10 @@ public object Message set { _message = JsonConvert.SerializeObject(value); } } - internal static AuditRecord Create(object message, string messageType, MessageAuditMetadata metadata) => - new AuditRecord + internal static AuditRecord Create(T message, string messageType, MessageAuditMetadata metadata) + where T : class + { + return new AuditRecord { ContextType = metadata.ContextType, MessageId = metadata.MessageId, @@ -94,5 +96,6 @@ public object Message Message = message, MessageType = messageType }; + } } } \ No newline at end of file diff --git a/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/EntityFrameworkAuditStore.cs b/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/EntityFrameworkAuditStore.cs index 6ccfc5a397..0f394f3543 100644 --- a/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/EntityFrameworkAuditStore.cs +++ b/src/Persistence/MassTransit.EntityFrameworkIntegration/Audit/EntityFrameworkAuditStore.cs @@ -1,23 +1,24 @@ // Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// +// this file except in compliance with the License. You may obtain a copy of the +// License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// // Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. -namespace MassTransit.EntityFrameworkIntegration +namespace MassTransit.EntityFrameworkIntegration.Audit { using System.Data.Entity; using System.Threading.Tasks; - using Audit; + using MassTransit.Audit; + using Util; - public class EntityFrameworkAuditStore : MessageAuditStore + public class EntityFrameworkAuditStore : IMessageAuditStore { readonly string _auditTableName; readonly string _nameOrConnectionString; @@ -31,13 +32,15 @@ public EntityFrameworkAuditStore(string nameOrConnectionString, string auditTabl public DbContext AuditContext => new AuditDbContext(_nameOrConnectionString, _auditTableName); - public async Task StoreMessage(object message, string messageType, MessageAuditMetadata metadata) + async Task IMessageAuditStore.StoreMessage(T message, MessageAuditMetadata metadata) { using (var dbContext = AuditContext) { - var auditRecord = AuditRecord.Create(message, messageType, metadata); + var auditRecord = AuditRecord.Create(message, TypeMetadataCache.ShortName, metadata); + dbContext.Set().Add(auditRecord); - await dbContext.SaveChangesAsync(); + + await dbContext.SaveChangesAsync().ConfigureAwait(false); } } } diff --git a/src/Persistence/MassTransit.EntityFrameworkIntegration/MassTransit.EntityFrameworkIntegration.csproj b/src/Persistence/MassTransit.EntityFrameworkIntegration/MassTransit.EntityFrameworkIntegration.csproj index 20d480237b..e0d5e6e37b 100644 --- a/src/Persistence/MassTransit.EntityFrameworkIntegration/MassTransit.EntityFrameworkIntegration.csproj +++ b/src/Persistence/MassTransit.EntityFrameworkIntegration/MassTransit.EntityFrameworkIntegration.csproj @@ -56,11 +56,8 @@ - - - @@ -84,10 +81,6 @@ - - {AE85151C-C049-47B2-BD8B-9915A3103299} - MassTransit.Audit - {6efd69fc-cbcc-4f85-aee0-efba73f4d273} MassTransit