Friday, 22 December 2017

What is log4net and how to use it in asp.net?


log4net: - 

    Apache library used to track the status, error and code execution level of your program.It can be used in asp.net and php.
    There are different logging levels like: -1.     OFF - nothing gets logged (cannot be called)
2.     FATAL
3.     ERROR
4.     WARN
5.     INFO
6.     DEBUG
7.     ALL - everything gets logged (cannot be called)
 Here I am going to use in ASP.net.
   STEPS to implement: -1.       Install log4net from NuGet Package Manager.2.       Now Set configuration in web.config if you are using web application or if using windows or console application set your configuration in app.config.3.       Write this simple code above your namespace of the class in which you are going to use like:-[assembly:log4net.Config.XmlConfigurator(Watch =true)]
 1:- To Install log4net:-
2. set configuraton in web.config

here i am using appender: FileAppender you can use other appender as well.

<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="D:\log.log" />
      <appendToFile value="true" />
     
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %type.%method - %message%n" />
      </layout>
    </appender>
    <root>
      <level value="All"/>
      <appender-ref ref="FileAppender" />
    </root>
</log4net>

3. Write Code like:-

[assembly:log4net.Config.XmlConfigurator(Watch =true)]
namespace WebScraping.Scriping
{
    public class Scrap
    {
      
private static readonly log4net.ILog log =    log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public Scrap(string url)
        {
            log.Error(new Exception("Testing"));
        }
}

finally you would get the file generated in you D drive which i have set in web.config.
to get more detail please visit:- 


Thursday, 21 December 2017

What is Finalize and how to implement it in C#?

Finalize:-

The cleanup and releasing the unmanaged resources we use finalize method.

If your class is not using unmanaged resources just forget about the finalize.

We cannot call finalize method explicitly like: abc.finalize() or anything. This method is called by Garbage collector.

When garbage collector comes into picture to remove the object and release the memory consumed by the object it checks if that object has Finalize method implemented it(GC) moves that object into finalize queue.

GC comes frequently to release the generation zero bucket. But when GC would come to release the finalize queue object we do not know.

So, checking of finalize queue is very less. So, that object does not remove if it is not in use. So, object still consume CPU memory.

Now how to implement finalize?

To implement finalize we use Destructor.

When your code compiles that Destructor converts into finalize method.

See the simple Employee class with destructor rounded there in the image: -

To show Finalize method i am using Telerik JustDecomplie software. See after compilation Employee class have Finalize() method which is called by the GC itself.


Wednesday, 20 December 2017

What is Web Scraping? How to do Web Scraping in C#?


What is Web Scraping?
             
       It is a technique to extract large amount of data from the websites. Data can be saved in Local system or in database tables.

This Scraping can be done manually but these days people are creating there own software to do the same task in few seconds or minutes.

If you have a website and you would like to get data you would extract the page by URL and parse the page by there elements, attributes or any selectors. and then saving then into your database or local drive.

How to implement Web Scraping in C#?

To start working on Web Scraping install package HtmlAgilityPack from Nuget like:-



If you visit the url :-

https://www.yellowpages.com/search?search_terms=software+develop&geo_location_terms=Sydney%2C+ND

I am trying to access the titles which circulated in the screenshot.




Use namespace HtmlAgilityPack.

using HtmlAgilityPack;



Once you run this code you would get the output like:-


Now you are good to start working on web scraping. Start diving into the namespace and classes and xpath more you would be able to fetch complex html easily.


Monday, 18 December 2017

What is .Net Obfuscating?


Best practice for .NET Obfuscating:-

To Obfuscate Assembly(.dll) in .Net install these tools first:-

  1. JustDecompile
  2. ConfuserEx

JustDecompile:- 
                 It is Telerik open source engine. It is used for 
  • See the source code and verify you yourself what the code does
  • You can study the code and learn the best way to write code as people are writing.
ConfuserEx:-
                          Features:

    • Anti debugger
    • Anti memory dumping
    • Anti decompiler
    • Prevent any tampering of the assemblies
    • Encrypt codes
    • Encrypt constants (i.e. numbers & strings)
    • Encrypt resources
    • Control flow obfuscation
    • External/Internal reference proxy
    • Renaming
Let's Take a simple "Hello World" string returning  assembly (.dll) and Obfuscating it and then try to Decompile it using Telerik JustDecompile Tool

I have class library named ClassLibrary1.

referencing it into the project ConsoleApplication1.

Now i am going to rebuild ClassLibrary1. Open Folder ClassLibrary1/bin/Debug here you will find ClassLibrary1.dll which we are going to obfuscate.



Once you have installed Confuser, open it.
Now drag ClassLibrary1.dll into Confuser.




Now go to Rules Tab and add your patterns. Here i have left it as it is and Preset to Minimum. Then Click OK.


Once you have clicked on OK. Now Click on Tab Confuse!. Automatically you would see your .dll will be obfuscated. Now switch over to ClassLibrary/bin/debug you would see Confused Folder Created.

Now i am trying to Decompile it using JustDecompile Tool.

Drag Original .dll file which was create first before Obfuscating.
You would be able to see the complete code.




After Obfuscating:-
In the above image you would find Hello world be converted into unreadable code.
Accordingly you can change the Confuser Rule and Preset. Again after Obfuscating you would be able to use the Obfuscated .dll to your running application.