Use Microsoft Unity for logging and unity Logging
Logs need to be recorded in the following aspects: when entering the method, when passing parameters, calculating the execution time, when the method returns parameters, when exiting the statement block, and when exceptions occur. First, try not to use Micirosoft Unity for logging.
class Program
{
static void Main(string[] args)
{
Add(1, 2);
Console.ReadKey();
}
private static int Add(int a, int b)
{
int result = 0;
string temp = string.Empty;
string returnValue = string.Empty;
try
{
// Record entry Method
Console. WriteLine ("method to be executed soon ");
Temp = string. Format ("input parameters: a = {0}, B = {1}", a, B );
Console.WriteLine(temp);
// Statistical method execution time
Stopwatch watch = new Stopwatch();
watch.Start();
result = a + b;
watch.Stop();
Console. WriteLine ("program execution time: {0}", watch. Elapsed );
// Record the return value
ReturnValue = string. Format ("returned result: {0}", result );
Console.WriteLine(returnValue);
// Receive the recording method execution
Console. WriteLine ("method execution ended ");
}
catch (Exception ex)
{
// Record exception
Console. WriteLine (string. Format ("exception message: {0}, input parameter: {1}", ex. ToString (), temp ));
throw;
}
finally
{
// Record Exception Handling
Console. WriteLine ("the exception has been handled ");
}
return result;
}
}
The above still has some problems:
○ Violation of the "DRY" principle. If there are other methods, you need to constantly write the record Logic
○ Impact on reading code
○ Time consumed
Microsoft Unity solves the above problems.
○ Proxy object or derived class is a Unity interceptor that intercepts methods before and after execution.
○ Behaviors Pipeline is the interception behavior Pipeline, registered through API
○ Target Object or Original class method is the Target Object for interception.
□Reference Unity and Unity. Interception Components
Enter the keyword Unity to install Unity through NuGet.
Enter the keyword Unity. Interception to install Unity Interception Extension through NuGet.
After installation, the related components include:
□Custom interceptor
The custom interceptor must implement the IInterceptionBehavior interface.
public class MyInterceptionBehavior : IInterceptionBehavior
{
// Return the interface required for interception
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
/// <summary>
/// Use this method to implement Interception
/// </summary>
/// <Param name = "input"> parameters of the target method </param>
/// <Param name = "getNext"> delegate the interception behavior in the interception pipeline </param>
/// <Returns> return value of the target method </returns>
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
Console. WriteLine ("hello, the method will be executed immediately ~~ ");
IMethodReturn msg = getNext()(input, getNext);
Console. WriteLine ("bye, method executed ");
return msg;
}
// Enable interception
public bool WillExecute
{
get { return true; }
}
}
□Define a computing Interface
public interface ICalculator
{
int Add(int value1, int value2);
int Subtract(int value1, int value2);
int Multiply(int value1, int value2);
int Divide(int value1, int value2);
}
□Interface implementation
public class Calculator : ICalculator
{
public int Add(int value1, int value2)
{
int res = value1 + value2;
Console.WriteLine(res);
return res;
}
public int Subtract(int value1, int value2)
{
int res = value1 - value2;
return res;
}
public int Multiply(int value1, int value2)
{
int res = value1 * value2;
return res;
}
public int Divide(int value1, int value2)
{
int res = value1 / value2;
return res;
}
}
□Configure Unity in the configuration file
<configuration>
<configSections>
<section
name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="ICalculator" type="MyLogging.ICalculator, MyLogging"/>
<alias alias="Calculator" type="MyLogging.Calculator, MyLogging"/>
<alias alias="MyBehavior" type="MyLogging.MyInterceptionBehavior, MyLogging" />
<sectionExtension
type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,
Microsoft.Practices.Unity.Interception.Configuration" />
<container>
<extension type="Interception"/>
<register type="ICalculator" mapTo="Calculator">
<interceptor type="InterfaceInterceptor" />
<interceptionBehavior type="MyBehavior"/>
</register>
</container>
</unity>
</configuration>
Above,
○ Use <alias> nodes to set aliases for interfaces and Classes
○ Type = "MyLogging. ICalculator, MyLogging", the class name is before the comma, and the Assembly name is after the comma.
□Client call
using System;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
class Program
{
static void Main(string[] args)
{
// Load the UnityContainer
IUnityContainer container = new UnityContainer();
container = Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(container);
// Parse the interface
ICalculator calc = Microsoft.Practices.Unity.UnityContainerExtensions.Resolve<ICalculator>(container);
// Execution Method
int res = calc.Add(1, 2);
Console.ReadKey();
}
}
References:
Http://www.lm-tech.it/Blog/post/2011/10/18/How-to-use-the-Unity-Interception-Extension.aspx
Where is the web player unity log?
Win7 is stored in C: \ Users \ Administrator \ AppData \ Local \ Temp \ UnityWebPlayer \ log
If a directory is hidden, it will be displayed.
Administrator is the login user. Select a folder based on your computer.
Xp, you can also guess where it is based on this directory.
Unity3D settings use VS2012 for script editing, but what should I do if Monodevelop is started every time?
I also encountered this problem ...... After that, we fixed vs again. After the fix, we started vs again and then connected it with unity!
The fix is almost equivalent to re-installing ...... Too time-consuming ......