I am using below code for creating logging in my project using PRISM ILoggrFacade.
But i am not sure is the best way and second thing how can i use CustomLogger class logging because i am unable to create logging.
public class CustomLogger : ILoggerFacade
{
protected static readonly ILog log = LogManager.GetLogger(typeof(CustomLogger));
public CustomLogger()
{
log4net.Config.XmlConfigurator.Configure();
}
public void Log(string message, Category category, Priority priority)
{
switch (category)
{
case Category.Debug:
log.Debug(message);
break;
case Category.Warn:
log.Warn(message);
break;
case Category.Exception:
log.Error(message);
break;
case Category.Info:
log.Info(message);
break;
}
}
}
Bootstrapper
private readonly CustomLogger _logger = new CustomLogger();
protected override ILoggerFacade CreateLogger()
{
return _logger;
}
ViewModel
logger.Log("Logging success", Category.Info, Priority.None);
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\temp\log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
Problem with above code is that need to configure log4net in assembly like below:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4Net.config", Watch = true)]
Also in addition if you wan to drag log4net in ViewModels the use below code
private readonly ILoggerFacade _loggerFacade;
public ContentAViewViewModel(ILoggerFacade loggerFacade)
{
_loggerFacade = loggerFacade;
}
Example
private void LoadPeople()
{
_loggerFacade.Log("Load People", Category.Debug, Priority.High);
}
Related
When I use log4j and TestNG #DataProvider I was able to generate logs for the below code, but when I use #DataProvider the logs are getting overwritten. I tried to use #BeforeTest but it's not working. Any idea on how can I generate for logs for all the my rows?
public class DemoTest extends baseFX {
public static Logger Log = LogManager.getLogger(baseFX.class.getName());
#Test(dataProvider="DataProvider", groups = { "baseFX" })
public void loginPageNav(String Test, String newMemEmail, String newMemPass ) throws Exception {
driver=driverSetup();
Log.info("Driver is initialized");
//Creating object for the Index to the class
IndexPage indexpage = new IndexPage(driver);
Log.info("Navigated to Index Page");
//invoke method
indexpage.clickLogin().click();
Log.info("Clicked on Login");
//Creating an object for the login page and invoke its elements
LoginPage loginpage = new LoginPage(driver);
loginpage.getPassword().sendKeys(password);
loginpage.loginBtn().click();
Log.info("Member successfully logged in");
//driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.close();
}
#DataProvider
public Object[][] DataProvider() throws Exception{
Object[][] arrayObject = ReadExcelData.getExcelData("./src/test/java/Autopkg/TestData/Demo.xlsx", "Sheet1");
return arrayObject;
}
//#AfterTest
//public void tearDown(){
//}
}
I use log4j configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="basePath">./logs</Property>
</Properties>
<Appenders>
<RollingFile name="File" fileName="${basePath}/prints.log" filePattern="${basePath}/prints-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<SizeBasedTriggeringPolicy size="1000" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
I've got a silverlight project and I'm trying to configure NLog for calling static method but it doesn't (using Nlog.config).
I'm following this example.
Here's Nlog.config code:
...
<targets>
<target name="m" xsi:type="MethodCall"
className="NLogTestSilver.MainPage, NLogTestSilver"
methodName="LogMethod">
<parameter layout="${level}" />
<parameter layout="${message}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="m" />
</rules>
...
Assembly name = NLogTestSilver.dll
Here's MainPage.xaml.cs code:
namespace NLogTestSilver
{
public partial class MainPage : UserControl
{
public static Logger Logger = LogManager.GetCurrentClassLogger();
public MainPage()
{
InitializeComponent();
Logger.Fatal("Fatality");
}
public static void LogMethod(string level, string message)
{
System.Windows.Browser.HtmlPage.Window.Alert(level + " " + message);
}
}
}
P.S. Programmatic configuration works well.
So it was found out that an exception is thrown in NLog.Targets.MethodCallTarget.InitializeTarget() method while processing className parameter.
If we change
className="NLogTestSilver.MainPage, NLogTestSilver"
to
className="NLogTestSilver.MainPage, NLogTestSilver,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx"
it works great.
Where xxxxxxxxxxxxxxxx is public key token of our assembley.
All ViewModel classes inherit from a base class:
public abstract class ScreenBase : ViewModelBase, IScreen, IDisposable
{
protected readonly NLog.Logger _logger;
protected ScreenBase()
: this(Messenger.Default) { }
protected ScreenBase(IMessenger messenger)
: base(messenger)
{
_logger = NLog.LogManager.GetLogger(this.GetType().Name);
_logger.Debug("{0} ({1}) constructed. ", this.GetType().Name, this.GetHashCode());
}
~ScreenBase()
{
FinalizeProc();
}
[Conditional("DEBUG")]
private void FinalizeProc()
{
_logger.Debug("{0} ({1}) Finalized. ", this.GetType().Name, this.GetHashCode());
}
}
As you can see, any time instance of ViewModel is created / destroyed, it should log it. I am logging it into Console window and file:
<targets>
<!-- add your targets here -->
<target name="logDebugInfo" xsi:type="File" deleteOldFileOnStartup="true" fileName="${specialfolder:folder=CommonApplicationData}/MyApp/debug.txt" layout="${longdate} | ${uppercase:${level}} | ${stacktrace} | ${message}${onexception:EXCEPTION OCCURRED\:${exception:format=tostring}}" />
<target name="console" xsi:type="Console" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Trace" maxlevel="Info" writeTo="logDebugInfo" />
<logger name="*" minlevel="Trace" writeTo="console" />
</rules>
Each message is properly logged until the application is closed. When 'X' Close button is pressed on the main view, I do not do anything specific in code, I let the application close itself. However, no 'Finalized' messages are displayed at that point.
Anyone knows why?
The reason why this is happening is that Logger is being destroyed on application shutdown.
I'm trying to enable solution-wide logging by adding a stand-alone project that wraps log4net. I found a code on StackOverflow but the code is using some config file. I do not understand that bit. Here is the only static class:
using log4net;
using log4net.Config;
using System;
using System.IO;
namespace ExciteEngine2.LoggingManager {
//// TODO: Implement the additional GetLogger method signatures and log4net.LogManager methods that are not seen below.
public static class ExciteLog {
private static readonly string LOG_CONFIG_FILE = #"log4net.config";
public static ILog GetLogger(Type type) {
// If no loggers have been created, load our own.
if (LogManager.GetCurrentLoggers().Length == 0) {
LoadConfig();
}
return LogManager.GetLogger(type);
}
private static void LoadConfig() {
//// TODO: Do exception handling for File access issues and supply sane defaults if it's unavailable.
try {
XmlConfigurator.ConfigureAndWatch(new FileInfo(LOG_CONFIG_FILE));
}
catch (Exception ex) {
}
}
}
}
Now, there is no log4net.config anywhere. And in my main application project, I'm using the ILog as follows:
using log4net;
using ExciteEngine2.LoggingManager;
namespace ExciteEngine2.MainApplication {
internal static class Program {
public static readonly ILog ApplicationLogger = ExciteLog.GetLogger(typeof(Program));
private static void SetupLogging() {
log4net.Config.XmlConfigurator.Configure();
}
[STAThread] static void Main(string[] args) {
//Uninstall
foreach (string arg in args) {
if (arg.Split('=')[0] == "/u") {
Process.Start(new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\msiexec.exe", "/x " + arg.Split('=')[1]));
return;
}
}
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
try {
ThemeResolutionService.ApplicationThemeName = ConfigurationManager.AppSettings["ThemeToUse"];
}
catch (Exception ex) {
ApplicationLogger.Error("Exception while setting Telerik Theme.", ex);
ThemeResolutionService.ApplicationThemeName = "ControlDefault";
}
DevExpress.UserSkins.OfficeSkins.Register();
DevExpress.UserSkins.BonusSkins.Register();
DevExpress.Skins.SkinManager.EnableFormSkins();
DevExpress.Skins.SkinManager.EnableMdiFormSkins();
//try {
if (args.Contains("/dx")) {
Application.Run(new AppMDIRibbonDX());
ApplicationLogger.Info("Application (DX) started.");
}
else {
Application.Run(new AppMDIRibbon());
ApplicationLogger.Info("Application started.");
}
//} catch (Exception ex) {
// ApplicationLogger.Fatal("Exception while initiating. Nothing can be done here.", ex);
// XtraMessageBox.Show(String.Format("Exception while initiating. Nothing can be done here.{0}Message: {1}", Environment.NewLine, ex.Message), "Excite Engine 2", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) {
ApplicationLogger.Fatal("Application Level Exception.", e.Exception);
Thread t = (Thread)sender;
Exception threadexception = e.Exception;
string errormessage = String.Format("Thread ID: {0} [ {1} ]", t.ManagedThreadId, threadexception.Message);
XtraMessageBox.Show(String.Format("Application Level Exception!{1}{0}{1}Details:{1}{2}", errormessage, Environment.NewLine, threadexception.StackTrace), "Excite Engine 2", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
As you can see from my flow, I'm executing this line of code thinking log4net will use my main application project's app.config: log4net.Config.XmlConfigurator.Configure();
And here is a line I added in AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Finally, the app.config for my Main application:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<appSettings>
</appSettings>
<connectionStrings>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="Excite Engine 2 Log.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{ddd, dd-MMM-yyyy hh:mm:ss} - %m%n" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Excite Engine 2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
When I run with <appender-ref ref="LogFileAppender" />, I get an empty file named Excite Engine 2 Log.log right next to my main EXE. And when I set <appender-ref ref="EventLogAppender" />, nothing happens in Event Viewer. Also, there is an attribute: <level value="DEBUG" /> thats really bothering me. What I want is a full EventViewer logging for my application regardless of the build configuration it is running in.
Appreciate if someone could guide me on this. Thanks!
I found a code on StackOverflow but
the code is using some config file. I
do not understand that bit.
The reason he's using a specific config file can be explained by the following taken from log4net's site:
The System.Configuration API is only
available if the configuration data is
in the application's config file; the
file named MyApp.exe.config or
Web.config. Because the
System.Configuration API does not
support reloading of the config file
the configuration settings cannot be
watched using the
log4net.Config.XmlConfigurator.ConfigureAndWatch
methods. The main advantage of using
the System.Configuration APIs to read
the configuration data is that it
requires less permissions than
accessing the configuration file
directly. The only way to configure an
application using the
System.Configuration APIs is to call
the
log4net.Config.XmlConfigurator.Configure()
method or the
log4net.Config.XmlConfigurator.Configure(ILoggerRepository)
method.
Edit:
To log to your log file you need to call your SetupLogging method above.
log4net.Config.XmlConfigurator.Configure();
This statement is never being called. It looks like you are calling LoadConfig() in your ExciteEngine2.LoggingManager but this uses a config file called log4net.config which you said doesn't exist. If you are putting your configuration in your app.config file then you need to call your SetupLogging method.
I need to authenticate users from database, Spring Security documents don't tell how to authenticate with hibernate. Is that possible and how can I do that?
You have to make your own custom authentication-provider.
Example code:
Service to load Users from Hibernate:
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
#Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
#Autowired private UserDao dao;
#Autowired private Assembler assembler;
#Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
UserDetails userDetails = null;
UserEntity userEntity = dao.findByName(username);
if (userEntity == null)
throw new UsernameNotFoundException("user not found");
return assembler.buildUserFromUserEntity(userEntity);
}
}
Service to convert your entity to a spring user object:
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
#Service("assembler")
public class Assembler {
#Transactional(readOnly = true)
User buildUserFromUserEntity(UserEntity userEntity) {
String username = userEntity.getName();
String password = userEntity.getPassword();
boolean enabled = userEntity.isActive();
boolean accountNonExpired = userEntity.isActive();
boolean credentialsNonExpired = userEntity.isActive();
boolean accountNonLocked = userEntity.isActive();
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (SecurityRoleEntity role : userEntity.getRoles()) {
authorities.add(new GrantedAuthorityImpl(role.getRoleName()));
}
User user = new User(username, password, enabled,
accountNonExpired, credentialsNonExpired, accountNonLocked, authorities, id);
return user;
}
}
The namespace-based application-context-security.xml would look something like:
<http>
<intercept-url pattern="/login.do*" filters="none"/>
<intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<form-login login-page="/login.do"
authentication-failure-url="/login.do?error=failed"
login-processing-url="/login-please.do" />
<logout logout-url="/logoff-please.do"
logout-success-url="/logoff.html" />
</http>
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService"/>
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.authentication.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
</beans:bean>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="md5"/>
</authentication-provider>
</authentication-manager>
If you're using a JDBC accessible database, then you could use the following authentication-provider and avoid creating a custom one. It cuts down the code required to 9 lines of XML:
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,password from users where username=?" authorities-by-username-query="select u.username, r.authority from users u, roles r where u.userid = r.userid and u.username =?" />
</authentication-provider>
You can then setup your dataSource as follows
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/DB_NAME" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
Have a look at this post: http://codehustler.org/blog/spring-security-tutorial-form-login/
It covers everything you need to know about customising Spring Security form-login.
A java configuration could look something like this
#Configuration
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
private UserDetailsServiceImpl userDetailsService;
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
DaoAuthenticationProvider daoAuthenticationProvider =
new DaoAuthenticationProvider();
daoAuthenticationProvider
.setUserDetailsService(userDetailsService);
auth.authenticationProvider(daoAuthenticationProvider);
}
}