Cred ca toata lumea stie la ce e bun Log4Net – este un logger . E foarte bun la log-at mesaje – in principiu cele de eroare – si trimis in cele mai diverse surse – la de fisiere text la console si la BD.
Dar problema este ca as fi vrut ceva sa imi logheze intrarea si iesirea dintr-o metoda, ceva de genul :
public string Arata(Point P, string s){
logger.Debug(“ am intrat cu Point “ + P + “ si string “ + s);
//code
logger.Debug(“ am iesit cu rezultatul “ + ….)
}
La recomandarea lui Dan Bunea, am dat de PostSharp si Log4PostSharp
Dupa ce m-am chinuit sa il fac sa lucreze , iata la ce am ajuns:
1. Un folder lib , in care am pus fisierele :
lib\a.txt
lib\Default.psproj
lib\Log4PostSharp.dll
lib\Log4PostSharp.pdb
lib\Log4PostSharp.psplugin
lib\Log4PostSharp.Weaver.dll
lib\plugins
lib\PostSharp-1.0.targets
lib\PostSharp-1.0.version
lib\PostSharp-AppDomain.config
lib\PostSharp-Library.config
lib\PostSharp-Platform.config
lib\PostSharp.Core.dll
lib\PostSharp.Core.XmlSerializers.dll
lib\PostSharp.exe
lib\PostSharp.exe.config
lib\PostSharp.Laos.dll
lib\PostSharp.Laos.psplugin
lib\PostSharp.Laos.Weaver.dll
lib\PostSharp.MSBuild.dll
lib\PostSharp.Public.dll
lib\PostSharp.targets
lib\plugins\Log4PostSharp.dll
lib\plugins\Log4PostSharp.pdb
lib\plugins\Log4PostSharp.psplugin
2. Project Properties=> Reference Paths=> adaugat folderul lib
3. Un fisier .psproj numit la fel ca proiectul, in care am scris standard
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<SearchPath Directory="bin/{$Configuration}"/>
<SearchPath Directory="{$SearchPath}" />
<SearchPath Directory="lib" />
<Tasks>
<AutoDetect />
<Compile TargetFile="{$Output}" IntermediateDirectory="{$IntermediateDirectory}" CleanIntermediate="false" />
</Tasks>
</Project>
4. In csproj adaugat cu mina, linga <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<DontImportPostSharp>True</DontImportPostSharp>
<PostSharpDirectory>lib\</PostSharpDirectory>
<PostSharpUseCommandLine>True</PostSharpUseCommandLine>
</PropertyGroup>
<Import Project="$(PostSharpDirectory)PostSharp.targets" Condition=" Exists(‘$(PostSharpDirectory)PostSharp.targets’) " />
5. In AssemblyInfo.cs adaugat (pentru a genera pentru fiecare metoda)
[assembly: Log(AttributeTargetTypes = "*", EntryLevel = LogLevel.Debug,
EntryText = "Entering method: {signature} with parameters {paramvalues}", ExitText = "exit method {signature} with {returnvalue}",
ExitLevel = LogLevel.Debug, ExceptionLevel = LogLevel.Error, AttributePriority = 1)]
Asta ca sa genereze un log pentru fiecare metoda …
6. Incepe log4net Adaugat un fisier log4net.config si pus
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("log4net.config"));
GATA! Configurat – si apoi am uitat de el….
Output-ul este ceva de genul :
2009-07-17 22:24:08,541 [TestRunnerThread] DEBUG fisier.cs Namespace.Clasa [(null)] – Entering method: Void .cctor() with parameters
2009-07-17 22:24:08,729 [TestRunnerThread] DEBUG fisier.cs Namespace.Clasa [(null)] – exit method Void .cctor() with
2009-07-17 22:24:08,760 [TestRunnerThread] DEBUG fisier.cs Namespace.Clasa [(null)] – Entering method: Namespace.ClasaList Find(System.String) with parameters "5888"
2009-07-17 22:24:08,760 [TestRunnerThread] DEBUG Namespace.ClasaList [(null)] – Entering method: Void .ctor() with parameters
2009-07-17 22:24:08,760 [TestRunnerThread] DEBUG Namespace.ClasaList [(null)] – exit method Void .ctor() with
2009-07-17 22:24:08,760 [TestRunnerThread] DEBUG clasa2.cs namespace.DataContext [(null)] – Entering method: Void .cctor() with parameters
2009-07-17 22:24:08,760 [TestRunnerThread] DEBUG clasa2.cs namespace.DataContext [(null)] – exit method Void .cctor() with
2009-07-17 22:24:09,166 [TestRunnerThread] DEBUG clasa2.cs namespace.DataContext [(null)] – Entering method: Void .ctor() with parameters
2009-07-17 22:24:09,166 [TestRunnerThread] DEBUG clasa2.cs namespace.DataContext [(null)] – exit method Void .ctor() with
2009-07-17 22:24:09,198 [TestRunnerThread] DEBUG clasa2.cs namespace.DataContext [(null)] – Entering method: System.Data.Linq.Table`1<Namespace.clasa1> get_clasa3() with parameters
2009-07-17 22:24:09,401 [TestRunnerThread] DEBUG clasa2.cs namespace.DataContext [(null)] – exit method System.Data.Linq.Table`1<Namespace.clasa1> get_clasa3() with Table(clasa2)
2009-07-17 22:24:11,307 [TestRunnerThread] DEBUG clasa2.cs namespace.DataContext [(null)] – Entering method: System.Data.Linq.Table`1<amespace.clasa2> get_clasa4() with parameters
2009-07-17 22:24:11,417 [TestRunnerThread] DEBUG clasa2.cs namespace.DataContext [(null)] – exit method System.Data.Linq.Table`1<amespace.clasa2> get_clasa4() with Table(clasa2)
2009-07-17 22:24:11,495 [TestRunnerThread] DEBUG fisier.cs Namespace.Clasa [(null)] – exit method Namespace.ClasaList Find(System.String) with Clasa2List : 0