##// END OF EJS Templates
Added Trace<T>.Debug(...) method for debug messages...
cin -
r280:f07be402ab02 v3
parent child
Show More
@@ -1,141 +1,152
1 using System;
1 using System;
2 using System.Collections.Generic;
2 using System.Collections.Generic;
3 using System.Diagnostics;
3 using System.Diagnostics;
4 using System.Linq;
4 using System.Linq;
5 using Implab.Components;
5 using Implab.Components;
6 using Implab.Diagnostics;
6 using Implab.Diagnostics;
7 using Implab.ServiceHost.Unity;
7 using Implab.ServiceHost.Unity;
8 using Implab.Xml;
8 using Implab.Xml;
9 using Unity;
9 using Unity;
10 using Unity.Injection;
10 using Unity.Injection;
11 using Unity.Registration;
11 using Unity.Registration;
12
12
13 namespace Implab.Playground {
13 namespace Implab.Playground {
14
14
15 public class Foo {
15 public class Foo {
16
16
17 public class Bar {
17 public class Bar {
18
18
19 }
19 }
20
20
21 public string Name { get; set; }
21 public string Name { get; set; }
22
22
23 public int IntValue { get; set; }
23 public int IntValue { get; set; }
24
24
25 public string StringValue { get; set; }
25 public string StringValue { get; set; }
26
26
27 public void AddRange(Foo[] items) {
27 public void AddRange(Foo[] items) {
28 Console.WriteLine($"AddRange: Foo[]");
28 Console.WriteLine($"AddRange: Foo[]");
29 }
29 }
30
30
31 }
31 }
32
32
33 public class FooFactory : IFactory<Foo>, IFactory<Foo.Bar> {
33 public class FooFactory : IFactory<Foo>, IFactory<Foo.Bar> {
34
34
35 public bool UseSsl { get; set; }
35 public bool UseSsl { get; set; }
36
36
37 public string Connection { get; set; }
37 public string Connection { get; set; }
38
38
39 public Foo Create() {
39 public Foo Create() {
40 return new Foo() {
40 return new Foo() {
41 Name = "AutoFac"
41 Name = "AutoFac"
42 };
42 };
43 }
43 }
44
44
45 Foo.Bar IFactory<Foo.Bar>.Create() {
45 Foo.Bar IFactory<Foo.Bar>.Create() {
46 return new Foo.Bar();
46 return new Foo.Bar();
47 }
47 }
48 }
48 }
49
49
50 public interface IContainer<T> {
50 public interface IContainer<T> {
51 T Instance { get; set; }
51 T Instance { get; set; }
52 }
52 }
53
53
54 public class Container<T> : IContainer<T> {
54 public class Container<T> : IContainer<T> {
55 public class Bar {
55 public class Bar {
56
56
57 }
57 }
58
58
59 public class Bar<T2> {
59 public class Bar<T2> {
60 public class Baz {
60 public class Baz {
61
61
62 }
62 }
63
63
64 }
64 }
65
65
66 public Container() {
66 public Container() {
67
67
68 }
68 }
69
69
70 public Container(T instance) {
70 public Container(T instance) {
71 Instance = instance;
71 Instance = instance;
72 }
72 }
73
73
74 public T Instance { get; set; }
74 public T Instance { get; set; }
75
75
76 public void SetInstance(T value) {
76 public void SetInstance(T value) {
77 Instance = value;
77 Instance = value;
78 }
78 }
79
79
80 public void AddRange(List<T> items) {
80 public void AddRange(List<T> items) {
81 Console.WriteLine($"AddRange: {typeof(List<T>)}");
81 Console.WriteLine($"AddRange: {typeof(List<T>)}");
82 }
82 }
83
83
84 public void AddRange(T[] items) {
84 public void AddRange(T[] items) {
85 Console.WriteLine($"AddRange: T[] ofType {typeof(T[])}");
85 Console.WriteLine($"AddRange: T[] ofType {typeof(T[])}");
86 }
86 }
87 }
87 }
88
88
89 public class Program {
89 public class Program {
90
90
91 static void Main(string[] args) {
91 static void Main(string[] args) {
92 var u1 = new Uri("/some/one");
93
94 Console.WriteLine($"{u1.IsAbsoluteUri}: {u1}");
95
96 var u2 = new Uri(u1, "../../two");
97
98 Console.WriteLine($"{u2.IsAbsoluteUri}: {u2}");
99
100 }
101
102 static void Main2(string[] args) {
92 var listener = new SimpleTraceListener(Console.Out);
103 var listener = new SimpleTraceListener(Console.Out);
93 var source = Trace<TypeResolver>.TraceSource;
104 var source = Trace<TypeResolver>.TraceSource;
94 source.Switch.Level = SourceLevels.All;
105 source.Switch.Level = SourceLevels.All;
95 source.Listeners.Add(listener);
106 source.Listeners.Add(listener);
96
107
97 var stopwatch = new Stopwatch();
108 var stopwatch = new Stopwatch();
98 stopwatch.Start();
109 stopwatch.Start();
99
110
100 var container = new UnityContainer();
111 var container = new UnityContainer();
101
112
102 Console.WriteLine($"Created: {stopwatch.ElapsedMilliseconds}");
113 Console.WriteLine($"Created: {stopwatch.ElapsedMilliseconds}");
103 stopwatch.Restart();
114 stopwatch.Restart();
104
115
105 container.LoadXmlConfiguration("data/sample.xml");
116 container.LoadXmlConfiguration("data/sample.xml");
106
117
107 Console.WriteLine($"Loaded: {stopwatch.ElapsedMilliseconds}");
118 Console.WriteLine($"Loaded: {stopwatch.ElapsedMilliseconds}");
108
119
109 stopwatch.Restart();
120 stopwatch.Restart();
110 var instace1 = container.Resolve<IContainer<string>>();
121 var instace1 = container.Resolve<IContainer<string>>();
111 Console.WriteLine($"Resolved1: {stopwatch.ElapsedMilliseconds}");
122 Console.WriteLine($"Resolved1: {stopwatch.ElapsedMilliseconds}");
112
123
113 stopwatch.Restart();
124 stopwatch.Restart();
114 var instace2 = container.Resolve<IContainer<Foo>>();
125 var instace2 = container.Resolve<IContainer<Foo>>();
115 Console.WriteLine($"Resolved2: {stopwatch.ElapsedMilliseconds}");
126 Console.WriteLine($"Resolved2: {stopwatch.ElapsedMilliseconds}");
116
127
117 DisplayContainerRegistrations(container);
128 DisplayContainerRegistrations(container);
118 }
129 }
119
130
120 static void DisplayContainerRegistrations(IUnityContainer theContainer) {
131 static void DisplayContainerRegistrations(IUnityContainer theContainer) {
121 string regName, regType, mapTo, lifetime;
132 string regName, regType, mapTo, lifetime;
122 Console.WriteLine("Container has {0} Registrations:",
133 Console.WriteLine("Container has {0} Registrations:",
123 theContainer.Registrations.Count());
134 theContainer.Registrations.Count());
124 foreach (ContainerRegistration item in theContainer.Registrations) {
135 foreach (ContainerRegistration item in theContainer.Registrations) {
125 regType = item.RegisteredType.FullName;
136 regType = item.RegisteredType.FullName;
126 mapTo = item.MappedToType.FullName;
137 mapTo = item.MappedToType.FullName;
127 regName = item.Name ?? "[default]";
138 regName = item.Name ?? "[default]";
128 lifetime = item.LifetimeManager.LifetimeType.Name;
139 lifetime = item.LifetimeManager.LifetimeType.Name;
129 if (mapTo != regType) {
140 if (mapTo != regType) {
130 mapTo = " -> " + mapTo;
141 mapTo = " -> " + mapTo;
131 } else {
142 } else {
132 mapTo = string.Empty;
143 mapTo = string.Empty;
133 }
144 }
134 lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length);
145 lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length);
135 Console.WriteLine("+ {0}{1} '{2}' {3}", regType, mapTo, regName, lifetime);
146 Console.WriteLine("+ {0}{1} '{2}' {3}", regType, mapTo, regName, lifetime);
136 }
147 }
137 }
148 }
138
149
139
150
140 }
151 }
141 }
152 }
@@ -1,111 +1,143
1 using System;
1 using System;
2 using System.IO;
2 using System.Reflection;
3 using System.Reflection;
4 using Implab.Diagnostics;
3 using Unity;
5 using Unity;
4
6
5 namespace Implab.ServiceHost.Unity {
7 namespace Implab.ServiceHost.Unity {
8 using static Trace<ContainerBuilder>;
9
6 public class ContainerBuilder {
10 public class ContainerBuilder {
7
11
8 readonly TypeResolver m_resolver;
12 readonly TypeResolver m_resolver;
9
13
10 readonly IUnityContainer m_container;
14 readonly IUnityContainer m_container;
11
15
12 readonly ContainerConfigurationSchema m_schema;
16 readonly ContainerConfigurationSchema m_schema;
13
17
18 Uri m_location;
19
14 public IUnityContainer Container {
20 public IUnityContainer Container {
15 get {
21 get {
16 return m_container;
22 return m_container;
17 }
23 }
18 }
24 }
19
25
20 public ContainerBuilder() : this(null, null) {
26 public ContainerBuilder() : this(null, null) {
21 }
27 }
22
28
23 public ContainerBuilder(IUnityContainer container, ContainerConfigurationSchema schema) {
29 public ContainerBuilder(IUnityContainer container, ContainerConfigurationSchema schema) {
24 m_container = container ?? new UnityContainer();
30 m_container = container ?? new UnityContainer();
25 m_resolver = new TypeResolver();
31 m_resolver = new TypeResolver();
26 m_schema = schema ?? ContainerConfigurationSchema.Default;
32 m_schema = schema ?? ContainerConfigurationSchema.Default;
27 }
33 }
28
34
29 public Type ResolveType(string typeReference) {
35 public Type ResolveType(string typeReference) {
30 return string.IsNullOrEmpty(typeReference) ? null : m_resolver.Resolve(typeReference, true);
36 return string.IsNullOrEmpty(typeReference) ? null : m_resolver.Resolve(typeReference, true);
31 }
37 }
32
38
33 public void Visit(ITypeRegistration registration) {
39 public void Visit(ITypeRegistration registration) {
34 Safe.ArgumentNotNull(registration, nameof(registration));
40 Safe.ArgumentNotNull(registration, nameof(registration));
35
41
36 var registrationType = registration.GetRegistrationType(this);
42 var registrationType = registration.GetRegistrationType(this);
37 var implementationType = registration.GetImplementationType(this) ?? registrationType;
43 var implementationType = registration.GetImplementationType(this) ?? registrationType;
38
44
39 if (registrationType == null)
45 if (registrationType == null)
40 throw new Exception($"A type must be specified for the registration {registration.Name}");
46 throw new Exception($"A type must be specified for the registration {registration.Name}");
41
47
42 var builder = new TypeRegistrationBuilder(
48 var builder = new TypeRegistrationBuilder(
43 m_resolver,
49 m_resolver,
44 registrationType,
50 registrationType,
45 implementationType
51 implementationType
46 );
52 );
47
53
48 builder.Lifetime = registration.GetLifetime(this);
54 builder.Lifetime = registration.GetLifetime(this);
49
55
50 if (registration.MemberInjections != null) {
56 if (registration.MemberInjections != null) {
51 foreach(var member in registration.MemberInjections)
57 foreach(var member in registration.MemberInjections)
52 member.Visit(builder);
58 member.Visit(builder);
53 }
59 }
54
60
55 m_container.RegisterType(
61 m_container.RegisterType(
56 builder.RegistrationType,
62 builder.RegistrationType,
57 builder.ImplementationType,
63 builder.ImplementationType,
58 registration.Name,
64 registration.Name,
59 builder.Lifetime,
65 builder.Lifetime,
60 builder.Injections
66 builder.Injections
61 );
67 );
62 }
68 }
63
69
64 public void Visit(IInstanceRegistration registration) {
70 public void Visit(IInstanceRegistration registration) {
65 Safe.ArgumentNotNull(registration, nameof(registration));
71 Safe.ArgumentNotNull(registration, nameof(registration));
66
72
67 var registrationType = registration.GetRegistrationType(this);
73 var registrationType = registration.GetRegistrationType(this);
68
74
69 var builder = new InstanceRegistrationBuilder (
75 var builder = new InstanceRegistrationBuilder (
70 m_resolver,
76 m_resolver,
71 registrationType
77 registrationType
72 );
78 );
73
79
74 builder.Lifetime = registration.GetLifetime(this);
80 builder.Lifetime = registration.GetLifetime(this);
75
81
76 if (registration.MemberInjections != null) {
82 if (registration.MemberInjections != null) {
77 foreach(var member in registration.MemberInjections)
83 foreach(var member in registration.MemberInjections)
78 member.Visit(builder.ValueBuilder);
84 member.Visit(builder.ValueBuilder);
79 }
85 }
80
86
81 if (builder.RegistrationType == null && builder.ValueBuilder.ValueType == null)
87 if (builder.RegistrationType == null && builder.ValueBuilder.ValueType == null)
82 throw new Exception($"A type must be specified for the registration {registration.Name}");
88 throw new Exception($"A type must be specified for the registration {registration.Name}");
83
89
84 m_container.RegisterInstance(
90 m_container.RegisterInstance(
85 builder.RegistrationType ?? builder.ValueBuilder.ValueType,
91 builder.RegistrationType ?? builder.ValueBuilder.ValueType,
86 registration.Name,
92 registration.Name,
87 builder.ValueBuilder.Value,
93 builder.ValueBuilder.Value,
88 builder.Lifetime
94 builder.Lifetime
89 );
95 );
90 }
96 }
91
97
92 public void AddNamespace(string ns) {
98 public void AddNamespace(string ns) {
93 m_resolver.AddNamespace(ns);
99 m_resolver.AddNamespace(ns);
94 }
100 }
95
101
96 public void AddAssembly(string assembly) {
102 public void AddAssembly(string assembly) {
97
103
98 }
104 }
99
105
106 /// <summary>
107 /// Includes the confguration. Creates a new <see cref="ContainerBuilder"/>,
108 /// and loads the configuration to it. The created builder will share the
109 /// container and will have its own isolated type resolver.
110 /// </summary>
111 /// <param name="file">A path to configuration relative to the current configuration.</param>
100 public void Include(string file) {
112 public void Include(string file) {
101 var includeContext = new ContainerBuilder(m_container, m_schema);
113 var includeContext = new ContainerBuilder(m_container, m_schema);
102 includeContext.LoadConfig(file);
114
115 if (m_location != null) {
116 var uri = new Uri(m_location, file);
117 includeContext.LoadConfig(uri);
118 } else {
119 includeContext.LoadConfig(file);
120 }
103 }
121 }
104
122
123 /// <summary>
124 /// Loads a configuration from the specified local file.
125 /// </summary>
126 /// <param name="file">The path to the configuration file.</param>
105 public void LoadConfig(string file) {
127 public void LoadConfig(string file) {
106 var config = m_schema.LoadFile(file);
128 Safe.ArgumentNotEmpty(file, nameof(file));
129
130 LoadConfig(new Uri(Path.GetFullPath(file)));
131 }
132
133 public void LoadConfig(Uri location) {
134 Safe.ArgumentNotNull(location, nameof(location));
135
136 m_location = location;
137
138 var config = m_schema.LoadConfig(location.ToString());
107 config.Visit(this);
139 config.Visit(this);
108 }
140 }
109
141
110 }
142 }
111 } No newline at end of file
143 }
@@ -1,70 +1,70
1 using System;
1 using System;
2 using System.Collections.Generic;
2 using System.Collections.Generic;
3 using System.IO;
3 using System.IO;
4 using System.Reflection;
4 using System.Reflection;
5 using System.Xml;
5 using System.Xml;
6 using System.Xml.Serialization;
6 using System.Xml.Serialization;
7 using Implab.Components;
7 using Implab.Components;
8
8
9 namespace Implab.ServiceHost.Unity {
9 namespace Implab.ServiceHost.Unity {
10 public class ContainerConfigurationSchema {
10 public class ContainerConfigurationSchema {
11
11
12 public static ContainerConfigurationSchema Default { get; private set; } = CreateDefault();
12 public static ContainerConfigurationSchema Default { get; private set; } = CreateDefault();
13
13
14 readonly LazyAndWeak<XmlSerializer> m_seralizer;
14 readonly LazyAndWeak<XmlSerializer> m_seralizer;
15
15
16 readonly XmlAttributeOverrides m_overrides = new XmlAttributeOverrides();
16 readonly XmlAttributeOverrides m_overrides = new XmlAttributeOverrides();
17
17
18 readonly XmlAttributes m_containerItems = new XmlAttributes();
18 readonly XmlAttributes m_containerItems = new XmlAttributes();
19
19
20 public XmlSerializer Serializer {
20 public XmlSerializer Serializer {
21 get {
21 get {
22 return m_seralizer.Value;
22 return m_seralizer.Value;
23 }
23 }
24 }
24 }
25
25
26 public ContainerConfigurationSchema() {
26 public ContainerConfigurationSchema() {
27 m_overrides.Add(typeof(ContainerElement), nameof(ContainerElement.Items), m_containerItems);
27 m_overrides.Add(typeof(ContainerElement), nameof(ContainerElement.Items), m_containerItems);
28
28
29 m_seralizer = new LazyAndWeak<XmlSerializer>(() => new XmlSerializer(typeof(ContainerElement), m_overrides));
29 m_seralizer = new LazyAndWeak<XmlSerializer>(() => new XmlSerializer(typeof(ContainerElement), m_overrides));
30 }
30 }
31
31
32 public void RegisterContainerElement(Type type, string name) {
32 public void RegisterContainerElement(Type type, string name) {
33 Safe.ArgumentNotNull(type, nameof(type));
33 Safe.ArgumentNotNull(type, nameof(type));
34 Safe.ArgumentNotEmpty(name, nameof(name));
34 Safe.ArgumentNotEmpty(name, nameof(name));
35
35
36 if(!type.IsSubclassOf(typeof(AbstractContainerItem)))
36 if(!type.IsSubclassOf(typeof(AbstractContainerItem)))
37 throw new Exception($"RegisterContainerElement '{name}': {type} must be subclass of {typeof(AbstractContainerItem)}");
37 throw new Exception($"RegisterContainerElement '{name}': {type} must be subclass of {typeof(AbstractContainerItem)}");
38
38
39 m_containerItems.XmlElements.Add(
39 m_containerItems.XmlElements.Add(
40 new XmlElementAttribute(name, type)
40 new XmlElementAttribute(name, type)
41 );
41 );
42 }
42 }
43
43
44 public void RegisterContainerElement<T>(string name) where T : AbstractContainerItem {
44 public void RegisterContainerElement<T>(string name) where T : AbstractContainerItem {
45 RegisterContainerElement(typeof(T), name);
45 RegisterContainerElement(typeof(T), name);
46 }
46 }
47
47
48 public ContainerElement LoadFile(string file) {
48 public ContainerElement LoadConfig(string uri) {
49 using (var reader = XmlReader.Create(file)) {
49 using (var reader = XmlReader.Create(uri)) {
50 return (ContainerElement)Serializer.Deserialize(reader);
50 return (ContainerElement)Serializer.Deserialize(reader);
51 }
51 }
52 }
52 }
53
53
54 static ContainerConfigurationSchema CreateDefault() {
54 static ContainerConfigurationSchema CreateDefault() {
55 var schema = new ContainerConfigurationSchema();
55 var schema = new ContainerConfigurationSchema();
56
56
57 schema.RegisterContainerElement<RegisterElement>("register");
57 schema.RegisterContainerElement<RegisterElement>("register");
58 schema.RegisterContainerElement<FactoryElement>("factory");
58 schema.RegisterContainerElement<FactoryElement>("factory");
59 schema.RegisterContainerElement<SerializedElement>("serialized");
59 schema.RegisterContainerElement<SerializedElement>("serialized");
60 schema.RegisterContainerElement<ValueElement>("value");
60 schema.RegisterContainerElement<ValueElement>("value");
61 schema.RegisterContainerElement<IncludeElement>("include");
61 schema.RegisterContainerElement<IncludeElement>("include");
62 schema.RegisterContainerElement<AssemblyElement>("assembly");
62 schema.RegisterContainerElement<AssemblyElement>("assembly");
63 schema.RegisterContainerElement<NamespaceElement>("namespace");
63 schema.RegisterContainerElement<NamespaceElement>("namespace");
64
64
65 return schema;
65 return schema;
66 }
66 }
67
67
68
68
69 }
69 }
70 } No newline at end of file
70 }
@@ -1,43 +1,27
1 using System;
1 using System;
2 using System.Collections.Generic;
2 using System.Collections.Generic;
3 using System.ComponentModel;
3 using System.ComponentModel;
4 using System.Linq;
4 using System.Linq;
5 using System.Xml.Serialization;
5 using System.Xml.Serialization;
6 using Implab.Xml;
6 using Implab.Xml;
7 using Unity.Injection;
7 using Unity.Injection;
8 using Unity.Lifetime;
8 using Unity.Lifetime;
9 using Unity.Registration;
9 using Unity.Registration;
10
10
11 namespace Implab.ServiceHost.Unity {
11 namespace Implab.ServiceHost.Unity {
12 /// <summary>
12 /// <summary>
13 /// Π‘Π°Π·ΠΎΠ²Ρ‹ΠΉ класс для формирования записСй Π² ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π΅, созволяСт ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ врСмя ΠΆΠΈΠ·Π½ΠΈ для записи
13 /// Π‘Π°Π·ΠΎΠ²Ρ‹ΠΉ класс для формирования записСй Π² ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π΅, созволяСт ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ врСмя ΠΆΠΈΠ·Π½ΠΈ для записи
14 /// </summary>
14 /// </summary>
15 public abstract class RegistrationBuilder {
15 public abstract class RegistrationBuilder {
16 public Type RegistrationType {
16 public Type RegistrationType {
17 get;
17 get;
18 private set;
18 private set;
19 }
19 }
20
20
21 internal LifetimeManager Lifetime { get; set; }
21 internal LifetimeManager Lifetime { get; set; }
22
22
23 protected RegistrationBuilder(Type registrationType) {
23 protected RegistrationBuilder(Type registrationType) {
24 RegistrationType = registrationType;
24 RegistrationType = registrationType;
25 }
25 }
26
27 internal void Visit(SingletonLifetimeElement simgletonLifetime) {
28 Lifetime = new SingletonLifetimeManager();
29 }
30
31 internal void Visit(ContainerLifetimeElement containerLifetime) {
32 Lifetime = new ContainerControlledLifetimeManager();
33 }
34
35 internal void Visit(HierarchicalLifetimeElement hierarchicalLifetime) {
36 Lifetime = new HierarchicalLifetimeManager();
37 }
38
39 internal void Visist(ContextLifetimeElement contextLifetime) {
40 Lifetime = new PerResolveLifetimeManager();
41 }
42 }
26 }
43 } No newline at end of file
27 }
@@ -1,132 +1,144
1 using System;
1 using System;
2 using System.Collections.Generic;
2 using System.Collections.Generic;
3 using System.Diagnostics;
3 using System.Diagnostics;
4 using System.Linq;
4 using System.Linq;
5 using System.Text;
5 using System.Text;
6 using System.Threading;
6 using System.Threading;
7 using System.Threading.Tasks;
7 using System.Threading.Tasks;
8
8
9 namespace Implab.Diagnostics {
9 namespace Implab.Diagnostics {
10 public static class Trace<T> {
10 public static class Trace<T> {
11
11
12 public static TraceSource TraceSource { get; } = new TraceSource(typeof(T).Name);
12 public static TraceSource TraceSource { get; } = new TraceSource(typeof(T).Name);
13
13
14 #if NETFX_TRACE_BUG
14 #if NETFX_TRACE_BUG
15 readonly static AsyncLocal<object> m_currentOperation = new AsyncLocal<object>();
15 readonly static AsyncLocal<object> m_currentOperation = new AsyncLocal<object>();
16 #endif
16 #endif
17
17
18 /// <summary>
18 /// <summary>
19 /// Starts the logical operation nested to the current operation nested to the current one.
19 /// Starts the logical operation nested to the current operation nested to the current one.
20 /// </summary>
20 /// </summary>
21 public static void StartLogicalOperation() {
21 public static void StartLogicalOperation() {
22 Trace.CorrelationManager.StartLogicalOperation();
22 Trace.CorrelationManager.StartLogicalOperation();
23
23
24 }
24 }
25
25
26 /// <summary>
26 /// <summary>
27 /// Starts the logical operation with the specified name, this name is usefull in logs.
27 /// Starts the logical operation with the specified name, this name is usefull in logs.
28 /// </summary>
28 /// </summary>
29 /// <param name="name">Name.</param>
29 /// <param name="name">Name.</param>
30 #if NETFX_TRACE_BUG
30 #if NETFX_TRACE_BUG
31 public static void StartLogicalOperation(object name) {
31 public static void StartLogicalOperation(object name) {
32 m_currentOperation.Value = name;
32 m_currentOperation.Value = name;
33 Trace.CorrelationManager.StartLogicalOperation(name);
33 Trace.CorrelationManager.StartLogicalOperation(name);
34 }
34 }
35 #else
35 #else
36 public static void StartLogicalOperation(object name) {
36 public static void StartLogicalOperation(object name) {
37 Trace.CorrelationManager.StartLogicalOperation(name);
37 Trace.CorrelationManager.StartLogicalOperation(name);
38 }
38 }
39 #endif
39 #endif
40
40
41 /// <summary>
41 /// <summary>
42 /// Ends the logical operation and restores the previous one.
42 /// Ends the logical operation and restores the previous one.
43 /// </summary>
43 /// </summary>
44 public static void StopLogicalOperation() {
44 public static void StopLogicalOperation() {
45 Trace.CorrelationManager.StopLogicalOperation();
45 Trace.CorrelationManager.StopLogicalOperation();
46 }
46 }
47
47
48 /// <summary>
48 /// <summary>
49 /// Writes a debug message.
50 /// </summary>
51 /// <param name="format">Format.</param>
52 /// <param name="arguments">Arguments.</param>
53 [Conditional("DEBUG")]
54 public static void Debug(string format, params object[] arguments) {
55
56 }
57
58 /// <summary>
49 /// Writes an informational message.
59 /// Writes an informational message.
50 /// </summary>
60 /// </summary>
51 /// <param name="format">Format.</param>
61 /// <param name="format">Format.</param>
52 /// <param name="arguments">Arguments.</param>
62 /// <param name="arguments">Arguments.</param>
53 [Conditional("TRACE")]
63 [Conditional("TRACE")]
54 public static void Log(string format, params object[] arguments) {
64 public static void Log(string format, params object[] arguments) {
55 TraceSource.TraceEvent(TraceEventType.Information, 0, format, arguments);
65 TraceSource.TraceEvent(TraceEventType.Information, 0, format, arguments);
56 }
66 }
57
67
58 /// <summary>
68 /// <summary>
59 /// Writes a warning message.
69 /// Writes a warning message.
60 /// </summary>
70 /// </summary>
61 /// <param name="format">Format.</param>
71 /// <param name="format">Format.</param>
62 /// <param name="arguments">Arguments.</param>
72 /// <param name="arguments">Arguments.</param>
63 [Conditional("TRACE")]
64 public static void Warn(string format, params object[] arguments) {
73 public static void Warn(string format, params object[] arguments) {
65 TraceSource.TraceEvent(TraceEventType.Warning, 0, format, arguments);
74 TraceSource.TraceEvent(TraceEventType.Warning, 0, format, arguments);
66 }
75 }
67
76
68 [Conditional("TRACE")]
77 /// <summary>
78 /// Writes a error message.
79 /// </summary>
80 /// <param name="format">Format.</param>
81 /// <param name="arguments">Arguments.</param>
69 public static void Error(string format, params object[] arguments) {
82 public static void Error(string format, params object[] arguments) {
70 TraceSource.TraceEvent(TraceEventType.Error, 0, format, arguments);
83 TraceSource.TraceEvent(TraceEventType.Error, 0, format, arguments);
71 }
84 }
72
85
73 [Conditional("TRACE")]
74 public static void Error(Exception err) {
86 public static void Error(Exception err) {
75 TraceSource.TraceData(TraceEventType.Error, 0, err);
87 TraceSource.TraceData(TraceEventType.Error, 0, err);
76 }
88 }
77
89
78 /// <summary>
90 /// <summary>
79 /// This method save the current activity, and transfers to the specified activity,
91 /// This method save the current activity, and transfers to the specified activity,
80 /// emits <see cref="TraceEventType.Start"/> and returns a scope of the new
92 /// emits <see cref="TraceEventType.Start"/> and returns a scope of the new
81 /// activity.
93 /// activity.
82 /// </summary>
94 /// </summary>
83 /// <param name="activityName">The name of the new activity/</param>
95 /// <param name="activityName">The name of the new activity/</param>
84 /// <param name="activityId">The identifier of the activity to which
96 /// <param name="activityId">The identifier of the activity to which
85 /// the control will be transferred</param>
97 /// the control will be transferred</param>
86 /// <returns>A scope of the new activity, dispose it to transfer
98 /// <returns>A scope of the new activity, dispose it to transfer
87 /// the control back to the original activity.</returns>
99 /// the control back to the original activity.</returns>
88 public static ActivityScope TransferActivity(string activityName, Guid activityId) {
100 public static ActivityScope TransferActivity(string activityName, Guid activityId) {
89 var prev = Trace.CorrelationManager.ActivityId;
101 var prev = Trace.CorrelationManager.ActivityId;
90
102
91 TraceSource.TraceTransfer(0, "Transfer", activityId);
103 TraceSource.TraceTransfer(0, "Transfer", activityId);
92 Trace.CorrelationManager.ActivityId = activityId;
104 Trace.CorrelationManager.ActivityId = activityId;
93 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName);
105 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName);
94
106
95 return new ActivityScope(TraceSource, prev, 0, activityName);
107 return new ActivityScope(TraceSource, prev, 0, activityName);
96 }
108 }
97
109
98 /// <summary>
110 /// <summary>
99 /// Emits <see cref="TraceEventType.Start"/> and returns a scope of the
111 /// Emits <see cref="TraceEventType.Start"/> and returns a scope of the
100 /// activity.
112 /// activity.
101 /// </summary>
113 /// </summary>
102 /// <param name="activityName">The name of the activity to start</param>
114 /// <param name="activityName">The name of the activity to start</param>
103 /// <returns>A scope of the new activity, dispose it to emit
115 /// <returns>A scope of the new activity, dispose it to emit
104 /// <see cref="TraceEventType.Stop"/> for the current activity.</returns>
116 /// <see cref="TraceEventType.Stop"/> for the current activity.</returns>
105 public static ActivityScope StartActivity(string activityName) {
117 public static ActivityScope StartActivity(string activityName) {
106 if (Trace.CorrelationManager.ActivityId == Guid.Empty)
118 if (Trace.CorrelationManager.ActivityId == Guid.Empty)
107 Trace.CorrelationManager.ActivityId = Guid.NewGuid();
119 Trace.CorrelationManager.ActivityId = Guid.NewGuid();
108
120
109 var prev = Trace.CorrelationManager.ActivityId;
121 var prev = Trace.CorrelationManager.ActivityId;
110
122
111 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName);
123 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName);
112 return new ActivityScope(TraceSource, prev, 0, activityName);
124 return new ActivityScope(TraceSource, prev, 0, activityName);
113 }
125 }
114
126
115 /// <summary>
127 /// <summary>
116 /// Creates new <see cref="LogicalOperation(string)"/> and calls
128 /// Creates new <see cref="LogicalOperation(string)"/> and calls
117 /// to <see cref="CorrelationManager.StartLogicalOperation(object)"/>
129 /// to <see cref="CorrelationManager.StartLogicalOperation(object)"/>
118 /// passing the created operation as identity. Calls
130 /// passing the created operation as identity. Calls
119 /// <see cref="TraceSource.TraceData(TraceEventType, int, object)"/>
131 /// <see cref="TraceSource.TraceData(TraceEventType, int, object)"/>
120 /// to notify listeners on operation start.
132 /// to notify listeners on operation start.
121 /// </summary>
133 /// </summary>
122 /// <param name="name">The name of the logical operation.</param>
134 /// <param name="name">The name of the logical operation.</param>
123 /// <returns>Logical operation scope, disposing it will stop
135 /// <returns>Logical operation scope, disposing it will stop
124 /// logical operation and notify trace listeners.</returns>
136 /// logical operation and notify trace listeners.</returns>
125 public static LogicalOperationScope LogicalOperation(string name) {
137 public static LogicalOperationScope LogicalOperation(string name) {
126 var operation = new LogicalOperation(name);
138 var operation = new LogicalOperation(name);
127 TraceSource.TraceData(TraceEventType.Information, TraceEventCodes.StartLogicalOperation, operation);
139 TraceSource.TraceData(TraceEventType.Information, TraceEventCodes.StartLogicalOperation, operation);
128 StartLogicalOperation(operation);
140 StartLogicalOperation(operation);
129 return new LogicalOperationScope(TraceSource, operation);
141 return new LogicalOperationScope(TraceSource, operation);
130 }
142 }
131 }
143 }
132 }
144 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now