Configuring Postsharp logging toolkit - c#

Using the free version of Postsharp, I added a logging aspect (using the toolkit, didn't code it myself). Later I changed my mind and wanted to log only upon entering a function, and not on leaving.
Where can this be configured? Couldn't find it anywhere.
Thanks!

Diagnostics configuration is stored in solution (.pssln) or project level configuration file (.psproj). These files may not contain required configuration tags or even they may be missing by default.
You can open the configuration wizard from smart tag over any method without [Log] attribute. The configuration you are interested in is on the first page (Logging Level). Note that there is "New logging profile..." at the bottom of this page - you can have multiple configurations within one application.
If you change diagnostics configuration then pssln file is created after completing the wizard and it should contain something like this:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration" xmlns:d="clr-namespace:PostSharp.Patterns.Diagnostics;assembly:PostSharp.Patterns.Diagnostics" xmlns:p="http://schemas.postsharp.org/1.0/configuration" xmlns:p1="http://schemas.postsharp.org/1.0/configuration">
<Property Name="LoggingEnabled" Value="{has-plugin('PostSharp.Patterns.Diagnostics')}" Deferred="true" />
<d:LoggingProfiles p:Condition="{$LoggingEnabled}">
<d:LoggingProfile Name="Default" OnExceptionLevel="None" OnSuccessLevel="None" />
</d:LoggingProfiles>
</Project>
The interesting tag is d:LoggingProfile with it's attributes OnExceptionLevel and OnSuccessLevel. You can add this configuration file manually as well.

Related

Android suppress ask permission for USB device

I know this has been asked as found USB Device Access on S/O. Additionally finding resource of USB Host connectivity. And even context Activity Declaration in Android Manifest here, so I have looked.
To clarify, this is to support a USB SmartCard Reader device attached to an Android device.
I am trying to create a stand-alone library that can be integrated with another Xamarin/Android application, but not providing full source to the library itself. That said, I created a new Solution "TestXamLibs". In it are the default two projects...
TestXamLibs
TestXamLibs.Android
My additional separate project that will be used as a Resource library (for context) is
XamCACReader
Having said that, the basic premise to summarize the others. I have a Xamarin/Android application that will utilize a USB device that can get attached and removed. The app itself which is not much more than a skeleton app of opening screen and a BroadcastReceiver to handle the listening and handling when such device is attached/detached.
When attached, I have to keep requesting permission to use the USB device over and over.
These other web posts identify how to handle it so you dont have to keep responding... YES, allow it again by applying some sugar within the application's manifest.
While trying to add that component, the program wont run. Remove it, and it runs. HOPEFULLY, something stupid simple. One additional caveat, I am working in Visual Studio and see the "Resources" folder in the project, but so many other places refer to the "res" folder explicitly and dont know if there is something else I am missing, or just because of the IDE of Visual Studio vs other Android based IDE.
Anyhow, below is the entire manifest including what I TRIED to implement to stop the nag prompting.
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="com.companyname.testxamlibs">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<application android:label="TestXamLibs.Android"
android:theme="#style/MainTheme"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application>
<activity>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#drawable/MyUsbDevice" />
</activity>
</application>
</manifest>
Then, in the Resources\Drawable folder, I added the "MyUsbDevice.xml" and its content below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="2278" product-id="13367" />
</resources>
Now, based on above, manifest, as it exactly sits above, when I try to run, I get a failure of
Severity Code Description Project File Line Suppression State
Error Missing 'name' key attribute on element activity at AndroidManifest.xml:34:5-39:16 TestXamLibs.Android
Another time I tried and had more information, but still did not work, it was nagging about
android:debuggable must be set to true
and obviously not able to debug.
Aside from needing the assistance to get this manifest correct, is there some other way directly within code that can bypass what this manifest is allowing otherwise? As having seen all the posts about same context, you might think there was another way.

Applying xml transformation to app.manifest

I just inherited a c# application. It currently has an entry in it's app.manifest to enable UAC
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Every time I do a debug build it inside visual studio, I get prompted that "This task requires the application to have elevated permissions". (I have an admin account but I don't logon with it when developing.)
Is there a ways to either apply a xml transformation to it (like on web.configs) or making a app.manifest for release mode?
Using the SlowCheetah NuGet package and accompanying Extension you will get the same behavior on all xml files as you have for web.config.
Be sure to install/activate the NuGet package as well as the Visual Studio Extension. Also, there are a number of Slow Cheetah versions in NuGet - I would suggest using the latest that is released by Microsoft - Microsoft.VisualStudio.SlowCheetah.
Read more on this: https://github.com/Microsoft/slow-cheetah
Edit: I had a long struggle actually getting the transform to work for App.Manifest.xml for my sharepoint add-in project. Turns out the files created for you when you use "Add transform" lack some details that if not included will cause the transform to fail (give no result). This is what I concluded:
<!-- Mandatory block in AppManifest transform files: -->
<App xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns="http://schemas.microsoft.com/sharepoint/2012/app/manifest"
Name="Not transformed"
ProductID="{12345678-0000-0000-0000-123456789123}"
Version="0.0.0.0"
SharePointMinVersion="0.0.0.0"
>
<Properties>
<Title>Not transformed</Title>
<StartPage>Not transformed</StartPage>
</Properties>
<AppPrincipal>
<RemoteWebApplication ClientId="*" />
</AppPrincipal>
</App>
<!--
This block as it is written will cause no transformation whatsoever, but all elements above must be present for any transformation to be applied.
To transform an entire element along with its text content, add the attribute xdt:Transform="Replace" to the element. This will also replace all
child elements.
-->
Hope this is of help!

Execute code only on localhost (when developing)

I do MVC app, I have few lines of code I would like to exucute only when I'm developing, and another, similar part of code when the application is deployed.
for example I have controller action that sends mail to some address, but I would like that adress is different when I am deploying and testing.
It is hard to change it always, and I often forget to do that.
Visual Studio provides a number of ways to do this, and which method you want to use depends on what exactly you want to do.
For example, you can use build targets to specify which type of build you want, Release and Debug are common, but you can also create others. You can then add #if pre-processor statements in your code to do things depending on which build is selected.
Another method, which sounds like what you want, is to use App Settings in your app or web.config. Then use the build transforms to transform your config based on the type of build (you will see a Web.Debug.config or Web.Release.config for instance. When you publish your site, Visual Studio will automatically apply these tranforms to your config files and change the app settings to whatever you want for that build type.
So, using your example, you would have this in your Web.config:
<appSettings>
<add key="notifyAddress" value="debug#foo.com" />
</appSettings>
Then, in your Web.Release.config you have this transform:
<appSettings>
<add key="notifyAddress" value="release#foo.com"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</appSettings>
In your code you have:
string emailAddress = ConfigurationManager.AppSettings["notifyAddress"];
Now, when you publish your site, emailAddress will automatically have release#foo.com.
see if this helps: https://stackoverflow.com/a/4597270/1043824. The recommended way will still be the config file approach.
In Visual Studio 2010, Request.ServerVariables("SERVER_SOFTWARE") will return Nothing (null) for the Development Server, and "Microsoft-IIS/7.5" for my Win7 Pro IIS/VS2010 installation.

C# + StyleCop + MSBuild + Global culture settings failing to be picked up

I've got my C# project file set up to import a StyleCop.Targets project that runs all the StyleCop rules on build. This is great and I've got the project down to zero errors when compiling through Visual Studio.
However when I compile through MSBuild (on the same machine) I get errors along the line of :
The documentation text within the constructor's summary tag must begin with the text: Initializes a new instance of the <see cref="MyClass" /> class.
Focus on the 'Z' in initialises... I've configured my Settings.StyleCop with an en-GB global setting so that I don't get errors about Americanisms within the code. However I can't fathom out why this causes errors in MSBuild.
I know MSBuild is using the same Settings.StyleCop file as if I change a rule (say TabsMustNotBeUsed) MSBuild (and Visual Studio) picks this change up and throws errors all over the place.
I am using StyleCop 4.7, Visual Studio 2012 and MSBuild 4.
Here is a snippet of my Settings.StyleCop file :
<StyleCopSettings Version="105">
<GlobalSettings>
<StringProperty Name="Culture">en-GB</StringProperty>
</GlobalSettings>
<Parsers>
<Parser ParserId="StyleCop.CSharp.CsParser">
<ParserSettings>
<BooleanProperty Name="AnalyzeDesignerFiles">False</BooleanProperty>
</ParserSettings>
</Parser>
</Parsers>
<Analyzers>
<Analyzer AnalyzerId="StyleCop.CSharp.SpacingRules">
<Rules>
<Rule Name="TabsMustNotBeUsed">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings />
</Analyzer>
</Analyzers>
</StyleCopSettings>
Any clues ?
Cheers!
I'm assuming you're using the latest version of StyleCop (4.7.41.0) or a version close to that. There have been a lot of changes and fixes in the 4.7 version line, and there have been fairly regular updates released.
I'm guessing that there must be another Settings.StyleCop file lower down (in a sub-folder) in the folder hierarchy that is setting the culture back to en-US. Modifying other rules in this particular settings file (like TabsMustNotBeUsed) will still behave as expected as long as you're not setting it again in the other settings file. A potential cause could be the working folder that StyleCop is being run from.
I suggest doing a quick scan in your file system to see if you can find any other settings files, and if found, check their culture setting.
Another trick I've done is to stop merging in settings files at the solution root. This can be done by adding the following setting:
<GlobalSettings>
<StringProperty Name="MergeSettingsFiles">NoMerge</StringProperty>
</GlobalSettings>
This will ensure that StyleCop acts the same on all development and build machines, regardless of the settings configured higher up the hierarchy (such as the one in the StyleCop application folder). However if you do this, make sure you copy all the required settings from the files no longer being merged. From your description I doubt that this will solve this particular problem, but I have found it useful to help retain rule consistency.

The key 'UserID' does not exist in the appSettings configuration section

All of a sudden I start getting this error while trying to open 2 of some 10+ forms in my Window Forms application in designer.
To prevent possible data loss before loading the designer, the following errors must be resolved:
The key 'UserID' does not exist in the appSettings configuration section.
It used to work fine and I dont' remember doing significant changes to it.
The key, of course, is in the appSettings alright, and always was, and the application builds and executes as expected. Only design view for these 2 forms is unaccessible.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Server" value="MYSERVER" />
<add key="DataBase" value="MYDB" />
<add key="UserID" value="MYUSER" />
<add key="PassWord" value="MYPASS" />
</appSettings>
</configuration>
One of them is just a Form, the other is a UserControl. None of them inherits from abstract classes or anything like that. Rebuilding or restarting Visual Studio does not help so far.
Any ideas on fixing it?
And finally, here is what the designer REALLY was complaining about:
I had a call to a stored procedure right from the User Control's InitializeComponent().
While it may not be a good idea indeed (separate question material?), I have to say that the error was not presented to me in the best possible way...
Is it possible the config file was moved to a different folder or a new config file was introduced somewhere?
Okay, there is something else in common with these 2 forms - they both use one UserControl and there is another error in the designer which says
"The variable 'myControl' is either undeclared or was never assigned."
(where myControl is the User Control).
Maybe I should manually delete it and try re-adding through the designer.
You might check to make sure your XML is well-formed. I'm relying on memory here, but I recall getting this error once after copying settings between different config files and the only problem was that I'd overwritten an extra angle bracket when pasting.

Categories