Archive

Since its inception, PostSharp had always allowed making Aspect-Oriented Programming in .NET easy, allowing developers to produce cleaner code, encapsulating infrastructure code behind reusable modules.

We are pleased to announce today that we are making this even easier – with PostSharp Toolkits!

What are PostSharp Toolkits?

PostSharp Toolkits are a collection of ready-made solutions for adding common infrastructure code, such as logging, exception handling and performance monitoring to your application with no changes to your source code! Powered by PostSharp, the most complete AOP solution for .NET, the PostSharp Toolkits build upon the raw power of Aspect-Oriented Programming to seamlessly apply those solutions throughout your application.

Sounds interesting, how does it work?

We have great examples on how to add things like logging, tracing, and exception handling to your code. We’ve built upon this knowledge, and added things like XML-configuration, so no changes in the source code are required! Simply grab the toolkit from NuGet, and you’re all set!

What toolkits currently exist?

With the first release, we’re introducing the PostSharp Diagnostics Toolkit – an instrumentation toolkit that adds diagnostics features, such as logging, exception handling, performance counters and feature tracking to your application. The PostSharp Diagnostics Toolkit includes pluggable support for the leading logging frameworks, such as NLog. Support for additional frameworks is coming soon!

Note: As this is a work in progress, the PostSharp Diagnostics Toolkit currently only has logging support, with a very limited feature set. In the following releases, we will introduce additional configuration options, support for popular frameworks and other cool stuff!

The source code for the PostSharp Toolkits is available on GitHub, and we’re going to introduce new features based on your feedback in short release cycles.

Getting started

Here is how to add logging to your application without changing a single line of code:

Starting with a simple project:

  • Step 1: Add the PostSharp Diagnostics Toolkit from NuGet to the assembly you wish to instrument. It downloads PostSharp automatically as a dependency (please note that you need PostSharp 2.1 SP1 or higher for the PostSharp Toolkits).

  • Step 2: Rebuild your application

When you now run your application, this is what is printed in the output:

How did this happen?

The NuGet installation creates a .psproj file named after the current project in the source tree. This is an XML file, containing the configuration, which is processed by PostSharp during compilation.

The details about the XML configuration will be posted on GitHub. For now, just note that in the <Data> section there is a definition of LogAttribute. This is equivalent of placing the LogAttribute (part of the toolkit) on an assembly level in the source code, via:

[assembly: LogAttribute]

By default, it will be applied to all methods of the application. You can use the Filtering Properties to limit the multicasting. Please refer to the Online Documentation for details.

If we take a look at the compiled assembly in our favorite decompiler, we can see that the output lines were added directly in the method bodies, and no reference to PostSharp.dll is required!

What about writing the output to a file?

Glad you asked! The PostSharp Diagnostics Toolkit for NLog is exactly the solution for writing the output to NLog. Install it from NuGet, it will download all the required dependencies automatically. You will then need to configure NLog (either manually, or by downloading a NuGet package NLog.Configuration, which contains a sample configuration file for NLog). Then simply rebuild your application, and NLog will be automatically added to it!

Few notes:

The PostSharp Toolkits project is built using the PostSharp SDK. The PostSharp SDK is completely unsupported and undocumented, for the reasons detailed in this blog post. While the source code is available on GitHub, any questions pertaining to the PostSharp SDK will go unanswered.

The PostSharp Toolkits is an ongoing project, we are aiming at short (2-week) release cycles, bringing you more features based on your feedback!

Questions? Suggestions? Bugs?

Please visit our dedicated PostSharp Toolkits Support Forum to let us know what you think!

Happy PostSharping!

-Igal

I’m happy we finally refreshed our front-page download and released PostSharp 2.1 SP 1, featuring 48 bug fixes and user stories.

All fixes were already available under the “download” section, so it’s not that anyone had to wait so long to get a solution.

This service packs solves virtually all defects that have been reported to us, most notably:

  • compatibility with Code Analysis (FxCop) – the infamous file locking issue
  • compatibility with Code Contracts (issues with debugging symbols)
  • compatibility with Silverlight 5
  • issues in PostSharp HQ when upgrading PostSharp

This makes this release a very stable one. For a list of all fixes, see the release notes.

The SP 1 contains the following new enhancements:

  • View source code enhanced by PostSharp from Visual Studio using your favorite decompiler.
  • Example code migrated to Visual Studio 2010 and cleaned up, licensing under BSD 2.0.
  • Refresh (actually, revival) of the XML project system:
    • Unification of plug-in configuration (psplugin) and project configuration (psproj).
    • Support for services that do not refer to PostSharp.Sdk.dll.
    • Support for data islands inside XML project files.
  • Adding aspects (actually, MulticastAttribute) to an XML project file – so without changing anything to source code.

The last few enhancements can appear a little cryptic to you, but they open the way to a new line of features we’ll blob about from next weeks: PostSharp Toolkits. They will make it easier to use the power of PostSharp without the difficulty of learning aspect-oriented programming.

Happy PostSharping!

-gael

Wow! What a turnout for my first webinar! Thank you all for attending, I had a great time presenting it, and will continue to do more webinars in the future. The webinar was recorded, and the video is now online for your viewing:

As promised, here are the answers to the great questions you asked, which I didn’t have the chance to answer during the live webinar. As always, you can always ask your questions on our Support Forums.

Q: Any reason the [aspect] class must be Serializable?

A: At (post) compilation, the aspect is serialized using binary serialization, and added as a resource to the target process. Then at runtime, the aspect is deserialized. PostSharp will emit a compilation error if you forget to mark your aspect as Serializable.

Q: What is the typical use case for creating interfaces using PostSharp?

A: One typical scenario for implementing interfaces with PostSharp is implementing INotifyPropertyChanged in WPF to support Data Binding.

Q: Are the OnExit and OnEnter aspects thread safe? How would one make it thread safe if it is not?

A: While the aspects themselves are thread-safe, shared state (i.e. custom fields) within the aspects is not. To safely pass data around between OnEnter and OnExit events, you can use the MethodExecutionArgs.MethodExecutionTag property.

Q: What about signed DLLs and PostSharp?

A: PostSharp has no problems with signed DLLs – they will be re-signed after the post-compilation process.

Q: What about Silverlight DLL's, is it also available?

A: Of course, PostSharp comes with the Silverlight assemblies included, you need to add a reference to the PostSharp.SL.dll instead of PostSharp.dll. For more information, please refer to the online documentation.

Q: Can you provide a good definition of aspect oriented programming?

A: Aspect Oriented Programming complements traditional OOP by allowing us to encapsulate infrastructure code (such as logging, security, validation, etc.) into small modules called aspects, and then apply those aspects throughout the entire system in such way that the business logic is completely separated from those concerns.

So go ahead, download the free starter edition now!

Happy PostSharping!

-Igal