WHAT'S NEW?
Loading...

NLog emails with several environments

Introduction


I've been playing a little bit with the logging extension that I use in one application at work. It's called NLog and it's an extended library used by lots of developers currently in its 3.2.0 version. I've put in place a solution to get emails whenever an error occurs. Basic knowlegde of NLog is required (targets and rules mainly).

1. NLog mails you errors


NLog is really well documented and you can find thousands of different examples to configure it. To add email functionality you have two options: add smtp details to your NLog conf file or get it directly from your app configuration file (if you have one). In my example I'm using GMail SMTP server.


I had another challenge, due to I work with different environments (dev, test, uat and prod), I don't want to receive emails in my inbox from none except the Production environment. For that reason I will configure some variables in nlog which will be populated just when the release output is selected in Visual Studio.

Please see here below how the App.Config file looks and the App.Release.Config file, the one for production:

<configsections>
     <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog">
   </section>
</configsections>
 
   <nlog autoreload="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.nlog-project.org/schemas/NLog.xsd">
     <variable name="emailSupportSubject" value="Issue in ENVIRONMENT">
     <variable name="supportEmailRecipients" value="">
     <targets>
       <target filename="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}}: ${message} ${exception:format=Message,StackTrace:innerFormat=Message,StackTrace:maxInnerExceptionLevel=9}" name="file" xsi:type="File">
       <target enablessl="true" from="XXXXX@gmail.com" layout="${longdate} ${uppercase:${level}}: ${message} ${exception:format=Message,StackTrace:innerFormat=Message,StackTrace:maxInnerExceptionLevel=9}" name="gmail" smtpauthentication="Basic" smtppassword="XXXX" smtpport="25" smtpserver="smtp.gmail.com" smtpusername="XXXXX@gmail.com" subject="${emailSupportSubject}" to="${supportEmailRecipients}" xsi:type="Mail">
     </target></target></targets>
     <rules>
       <logger minlevel="Info" name="*" writeto="file">
       <logger minlevel="Error" name="*" writeto="gmail">
     </logger></logger></rules>
   </variable></variable></nlog>

App.Config. Here first, we are defining a new section to configure nlog before "...", then we define two new targets, one is used to write in a file and the second one is used to send an email with the normal Google smtp configuration. As you can see we've defined two variables one for the Subject ("emailSupportSubject") of our mails and other for the recipients ("supportEmailRecipients"), empty, by now, because the recipients will be populated in the next example.

We've setup some rules for both targets. For the file we will be loggin an "Info" level which means we just want to write in our file just both messages which contains normal information about what's going on in our app. For the email we want to get just the errors, and that´s the value we setup in the parameter: minLevel.

App.Release.Config. At this stage we just need to replace the values of the two variables we've just created in our previous file and every time we make a deployment into production we'll get emails just when an error appears.

2. NLog tricks

In this last section, I want to show you some tricks you can use with NLog to get good feedback about how are the things going in your applications.

2.1 Automatic log rotation. This is a very usefull parameter if you want to avoid huge log files in your environments. You can delegate this responsability directly into NLog by using some parameters. Please see here below an example of how to log 7 days of activities.


    <!-- 
  See http://nlog-project.org/wiki/Configuration_file 
  for information on customizing logging rules and outputs.
   -->
    <targets>
        <!-- add your targets here -->
        <target archiveevery="Day" archivefilename="${basedir}/logs/archive.{#}.log" archivenumbering="Rolling" concurrentwrites="true" encoding="iso-8859-2" filename="${basedir}/logs/current.log" keepfileopen="false" layout="${longdate} ${logger} ${message} ${exception:format=tostring}" maxarchivefiles="7" name="f" xsi:type="File">
    </target></targets>
 
    <!-- Add your logging rules here.  -->
    <rules>
        <logger minlevel="Info" name="*" writeto="f">
    </logger></rules>
</nlog>

2.2 Log entire objects. Using ServiceStack.Text you can log entire objects by serializing them and post them in any target you define in NLog. Just install the package by "Install-Package" command in the Package Manager console. Then you can start using commands like this:

logger.Debug("Initializing settings with object: {0}", someobject.Dump());

For this an more examples go to the references here below and you will find more interesting usages of this great loggin tool as: how to log all network traffic, loggin exception updating your layout and how to find where your log is programmatically.


References
http://nlog-project.org/ 
http://www.danesparza.net/2014/06/things-your-dad-never-told-you-about-nlog/

Can't connect my android device in debug mode

I've been playing a little bit with some mobile development technologies like Cordova, PhoneGap, VS Cordova extension, Android Studio,... And at some point, you will need to test your application in your real device instead of an emulator. After testing different Android devices, I was not able to detect my phone connected to my PC because it was not able to recognize the device. Something was wrong with the USB drivers.

Fortunately, I found a really good post by Kannon Yamad which explains perfectly what you need to do. I've added a small brief with some steps here:

  1. First, you need to uninstall your current device, connecting your phone and going to your device manager. There you just need to make a right click and uninstall the driver.
  2. The next step you need to achieve is, clean your PC of drivers you don't really need. First, download the tool Nirsoft's USB Devices View Utility and delete all the drivers with the words: “Google”, “Linux”, “ADB”, or “Android”.
  3. Finally, you just need to install the universal Android driver called "Koush's Universal android driver" and plug your device.

This process will make your android development experience a lot better, I lost a lot of time on this issue and I find this universal driver really useful working on phones and tablets without problem.

Hope you enjoyed this post and see you soon!

References: 

Take the challenge and boost your coding skills!