Search This Blog

Wednesday, July 19, 2017

Unity And seriLogger Example C#


using Microsoft.Practices.Unity;
using Serilog;
using Serilog.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnityAndLoggerExample
{

    /// <summary>
    /// https://csbubblog.wordpress.com/2013/06/22/unity-registration/
    /// </summary>
    class Program
    {
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Verbose()
                   .WriteTo.Logger(l => l
                        .MinimumLevel.Verbose()
                         .WriteTo.Logger(l2 => l2
                             .WriteTo.Logger(l3 => l3
                             .Filter.ByIncludingOnly(Matching.FromSource<Product>())
                             .MinimumLevel.Verbose()
                            .WriteTo.RollingFile(@"D:\Product-{Date}.txt"))
                           )
                        .WriteTo.Logger(l2 => l2
                           .WriteTo.Logger(l3 => l3
                           .Filter.ByIncludingOnly(Matching.FromSource<Product1>())
                           .MinimumLevel.Verbose()
                           .WriteTo.RollingFile(@"D:\Product1-{Date}.txt"))
                        )
                        .WriteTo.Logger(l2 => l2
                           .WriteTo.Logger(l3 => l3
                           .Filter.ByIncludingOnly(Matching.FromSource<class1>())
                           .MinimumLevel.Verbose()
                           .WriteTo.RollingFile(@"D:\class1-{Date}.txt"))
                        )
                          .WriteTo.Logger(l2 => l2
                           .WriteTo.Logger(l3 => l3
                           .Filter.ByIncludingOnly(Matching.FromSource<class2>())
                           .MinimumLevel.Verbose()
                           .WriteTo.RollingFile(@"D:\class2-{Date}.txt"))
                        )
                 .WriteTo.RollingFile(@"D:\Log-{Date}.txt"))
                .CreateLogger();

            UnityContainer IU = new UnityContainer();
            IU.RegisterType<class1>();
            IU.RegisterType<class2>();
            /* Register a type with specific members to be injected. */
            IU.RegisterType<IProduct, Product1>();
            IU.RegisterInstance<ILogger>(Log.Logger);

            IU.Resolve<Product>().Insertdata();
            IU.Resolve<Product1>().Insertdata();
            IU.Resolve<class1>().Insert1();
            IU.Resolve<class2>().Insert2();
        }
        public class class1
        {
            private IProduct _objpro;
            private ILogger _logger;

            public class1(IProduct objpro, ILogger logger)
            {
                _objpro = objpro;
                _logger = logger.ForContext<class1>();
            }

            public void Insert1()
            {
                // Methods starts
                _objpro.Insertdata();
                _logger.Warning("class1 warning log");
                // Method Ends
            }
        }
        public class class2
        {
            private IProduct _objpro;
            private ILogger _logger;

            public class2(IProduct objpro, ILogger logger)
            {
                _objpro = objpro;
                _logger = logger.ForContext<class2>();
            }

            public void Insert2()
            {
                _objpro.Insertdata();
                _logger.Warning("class2 warning log");
            }

        }
        public interface IProduct
        {
            string Insertdata();
        }
        public class Product : IProduct
        {
            ILogger _logger;
            public Product(ILogger logger)
            {
                _logger = logger.ForContext<Product>();
            }
            public string Insertdata()
            {
                _logger.Warning("Product warning log");

                return "true";
            }
        }

        public class Product1 : IProduct
        {
            ILogger _logger;
            public Product1(ILogger logger)
            {
                _logger = logger;
                _logger = logger.ForContext<Product1>();
            }
            public string Insertdata()
            {
                _logger.Warning("Product1 warning log");
                return "true";
            }
        }


    }
}


No comments:

Post a Comment