private static void Main() { var log = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.Console() .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 512) .CreateLogger(); log.Information("Hello, Serilog!"); var position = new { Latitude = 25, Longitude = 134 }; var elapsedMs = 34; var products = new List <string> { "Paper", "Pencil", "Pen" }; log.Information("Processed {Position} in {Elapsed} ms.", position, elapsedMs); log.Information("Ordered {@products}", products); log.Information("Added {UserName}", "Sarah"); log.Information("Added {UserName:l}", "Sarah"); log.Information("PI is {PI}", Math.PI); log.Information("PI is {PI:0.00}", Math.PI); log.Verbose("This is verbose."); log.Debug("This is debug."); log.Warning("This is warning"); log.Error("This is error"); log.Fatal("This is Fatal"); Log.CloseAndFlush(); Console.ReadKey(); }
static void Main(string[] args) { //Configuration by AppSettings var logger = new LoggerConfiguration() .ReadFrom.AppSettings() .MinimumLevel.Debug() .Enrich.WithThreadId() .Enrich.WithProperty("MyMetaProperty", "Oh! the beautiful value!") .WriteTo.ColoredConsole() .CreateLogger(); ////Configuration by code //var logger = new LoggerConfiguration() // .MinimumLevel.Debug() // .Enrich.WithThreadId() // .Enrich.WithProperty("MyMetaProperty", "Oh! the beautiful value!") // .WriteTo.ColoredConsole() // .WriteTo.BrowserConsole(port: 9999, buffer: 50) // .CreateLogger(); OpenBrowserToBrowserLogUrl(); logger.Information("Hello!"); Thread.Sleep(1000); for (int i = 0; i < 100000; i++) { logger.Information("Hello this is a log from a server-side process!"); Thread.Sleep(100); logger.Warning("Hello this is a warning from a server-side process!"); logger.Debug("... and here is another log again ({IndexLoop})", i); Thread.Sleep(200); try { ThrowExceptionWithStackTrace(4); } catch (Exception ex) { logger.Error(ex, "An error has occured, really?"); } Thread.Sleep(1000); } }
static void Main(string[] args) { //Create Logger var logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.ColoredConsole() .WriteTo.RollingFile(@"C:\Users\Daniel Contreras\source\repos\Serilog\Serilog\Log-{Date}.txt") .CreateLogger(); // prepare data var order = new { Id = 12, Total = 128.50, CustomerId = 72 }; var customer = new { Id = 72, Name = "John Smith" }; // write log message logger.Information("New orders {OrderId} by {Customer}", order.Id, customer); logger.Debug("Debugging message"); logger.Information("Information message"); logger.Warning("Warning message"); logger.Error("Error message"); logger.Fatal("Fatal message"); Console.ReadKey(); }
protected override bool AuthorizeCore(HttpContextBase httpContext) { bool disableAuthentication = false; GenericIdentity identity; GenericPrincipal principal; string[] roles; #if DEBUG disableAuthentication = true; #endif if (disableAuthentication) { // Create a fake user and use this in debugging to disable authentication. identity = new GenericIdentity("alongoria"); roles = new string[] { "admin" }; principal = new GenericPrincipal(identity, roles); httpContext.User = principal; } else { HttpCookie authCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); string username = authTicket.Name; // According to many articles and Ninject docs like: // https://github.com/ninject/Ninject.Web.Mvc/wiki/Filters-and-Scoped, // https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=98, // https://stackoverflow.com/questions/29915192/unity-property-injection-on-authorizeattribute // MVC caches filters, which means dependency injection with .InRequestScope() does not work. // If we try to inject the _authorizationFilter with Ninject, there is a runtime error saying the DbContext has been disposed // on the second and all subsequent requests (works fine on the first request). using (_userService = new UserService()) { User user = _userService.GetByUsername(username); if (user == null) { // The user has a cookie, so they are in the Active Directory. // But they aren't in our local database (new employee, probably), so add them. User newUser = new User() { Username = username, IsActive = true, IsAdmin = false }; HttpCookie initialsCookie = httpContext.Request.Cookies[Configuration.GetAppSetting("UserInitialsCookieName")]; if (initialsCookie != null && !string.IsNullOrWhiteSpace(initialsCookie.Value)) { newUser.Initials = initialsCookie.Value; } else { if (username.Length > 1) newUser.Initials = username.Substring(0, 2); else newUser.Initials = "xx"; } _userService.Insert(newUser); user = _userService.GetByUsername(username); using (var log = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger()) { log.Information("A new user was added to the application: {0}", username); } } // End inserting new User and pulling it from the db. else { // If there's a cookie with initials, check its value to make sure our db value isn't out of date. HttpCookie initialsCookie = httpContext.Request.Cookies[Configuration.GetAppSetting("UserInitialsCookieName")]; if (initialsCookie != null && !string.IsNullOrWhiteSpace(initialsCookie.Value)) { if (!initialsCookie.Value.Equals(user.Initials, System.StringComparison.CurrentCultureIgnoreCase)) { user.Initials = initialsCookie.Value; _userService.Update(user); } } } if (!user.IsActive) { using (var log = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger()) { log.Warning("A user whose account was disabled attempted to log on to the application: {0}", username); } throw new HttpException(500, string.Format("The {0} account has been disabled.", authTicket.Name)); } if (user.IsAdmin) roles = new string[] { "admin" }; else roles = null; } identity = new GenericIdentity(username, "Forms"); principal = new GenericPrincipal(identity, roles); httpContext.User = principal; } } // Now that we have set httpContext.User appropriately, do the authorization check which will make sure user is in the proper Role. return base.AuthorizeCore(httpContext); }