Having trouble getting the database target for NLog to work. I have 2 targets: FILE and DB.. here is my NLog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwExceptions="false">
<variable name="appName" value="KoreCMS" />
<targets async="true">
<target xsi:type="File"
name="default"
layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}"
fileName="${basedir}\App_Data\Log\Debug.log"
keepFileOpen="false"
archiveFileName="${basedir}\App_Data\Log\Debug_${shortdate}.{##}.log"
archiveNumbering="Sequence"
archiveEvery="Day"
maxArchiveFiles="30"/>
<target xsi:type="Database"
name="database"
keepConnection="true"
useTransactions="true"
dbProvider="System.Data.SqlClient"
connectionStringName="DefaultConnection"
commandText="INSERT INTO Kore_Log(EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES (#EventDateTime, #EventLevel, #UserName, #MachineName, #EventMessage, #ErrorSource, #ErrorClass, #ErrorMethod, #ErrorMessage, #InnerErrorMessage)">
<parameter name="#EventDateTime" layout="${date:s}" />
<parameter name="#EventLevel" layout="${level}" />
<parameter name="#UserName" layout="${aspnet-user-identity}" />
<parameter name="#MachineName" layout="${machinename}" />
<parameter name="#EventMessage" layout="${message}" />
<parameter name="#ErrorSource" layout="${event-context:item=error-source}" />
<parameter name="#ErrorClass" layout="${event-context:item=error-class}" />
<parameter name="#ErrorMethod" layout="${event-context:item=error-method}" />
<parameter name="#ErrorMessage" layout="${event-context:item=error-message}" />
<parameter name="#InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
</target>
</targets>
<rules>
<logger name="*" writeTo="default" minlevel="Info" />
<logger name="*" writeTo="database" minlevel="Info" />
</rules>
</nlog>
And here is the DB design:
And here is the connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=.\SQLExpress2012;Initial Catalog=MyDatabaseName;Integrated Security=True;Persist Security Info=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
What am I missing?
Alright, I figured it out... When I opened up SQL Profiler, I could see the INSERT command was indeed being sent, so I attempted to run the same command myself manually and discovered that I was being an idiot; basically, my Id column is setup as a uniqueidentifier and so of course it could not be auto incremented. The solution was to change the commandText in the Nlog.config to the following:
commandText="INSERT INTO Kore_Log(Id, EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES (NEWID(), #EventDateTime, #EventLevel, #UserName, #MachineName, #EventMessage, #ErrorSource, #ErrorClass, #ErrorMethod, #ErrorMessage, #InnerErrorMessage)">
I got into same problem, reason was dbProvider value and connectionStringName key in nlog.config. I got it from here
<target name="database" xsi:type="Database" connectionString="${configsetting:name=sqlconnection.connectionString}"
dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient">
Also,
connectionString should be used in place of connectionStringName
if Microsoft.Data.SqlClient is not added in dependency, it need to be added either using nuget package manager or manually.
Related
I get a MVC website and with the log4net to record the log.
In the web application, I store the user information in session, add log some information after executing one operation. Then the session lost when I returned to the home page(just type the url of the home page).
I am sure that the session lost which has no relation to the "Response.Redirect()" or something else, because I test the application and found it will recover after I comment the lockingModel property which value is "log4net.Appender.FileAppender+MinimalLock" in log4net config file.
Below is the configuration:
<?xml version="1.0"?>
<log4net debug="true">
<appender name="AllInfoRollingAppender" type="log4net.Appender.RollingFileAppender">
<file value="bin\\Log\\AllInfo-UniqueBlog-" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<staticLogFileName value="false" />
<rollingStyle value="Composite" />
<datePattern value="yyyy-MM-dd.LOG" />
<maximumFileSize value="1M" />
<maxSizeRollBackups value="4" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline%exception" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="AllInfoRollingAppender" />
</root>
</log4net>
So I think the "lockingModel" property in log4net config cause this error, but i don't know the reason, and it will not throw any error there, does anyone encounter this? I found it is inconceivable because this shouldn't cause this issue.
Your question is totally not readable. However I have to things you can check. If your session is lost, do you log before or after the Session.Redirec(). If you log after the session redirect, your code is never hit. The redirect ends your current thread. Next thing you can enable log4net debugging:
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
...
</configuration>
If something bad happens in log4net, you will see it in the logs.
I need to check each file in directory "/home/user/from/" with com.wso2esb.command.MyCommand and if the result == true, replace file to "home/user/to/". I have the following proxy:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="fromto"
transports="vfs"
statistics="enable"
trace="enable"
startOnLoad="true">
<target>
<inSequence>
<pojoCommand name="com.wso2esb.command.MyCommand">
<axis2ns47:property xmlns:axis2ns47="http://ws.apache.org/ns/synapse"
name="fileURL"
expression="$trp:FILE_PATH"
action="ReadMessage"/>
<axis2ns48:property xmlns:axis2ns48="http://ws.apache.org/ns/synapse"
name="result"
context-name="com.fx.FileIsOBI"
action="UpdateContext"/>
</pojoCommand>
<switch source="$ctx:com.fx.FileIsOBI">
<case regex="^true$">
<log level="custom">
<property name="OUTPUT"
expression="fn:concat('===555===', $ctx:com.fx.FileIsOBI)"/>
</log>
<property name="OUT_ONLY" value="true" scope="default"/>
<property xmlns:ns="http://org.apache.synapse/xsd"
xmlns:ns3="http://org.apache.synapse/xsd"
name="transport.vfs.ReplyFileName"
expression="$trp:FILE_NAME"
scope="transport"/>
<send>
<endpoint>
<address uri="vfs:file:////home/user/to"/>
</endpoint>
</send>
</case>
</switch>
</inSequence>
</target>
<parameter name="transport.PollInterval">10</parameter>
<parameter name="transport.vfs.ActionAfterProcess">DELETE</parameter>
<parameter name="transport.vfs.FileURI">vfs:file:////home/user/from/</parameter>
<parameter name="transport.vfs.FileNamePattern">.*\.txt</parameter>
<parameter name="transport.vfs.ContentType">application/octet-stream</parameter>
<parameter name="transport.vfs.ActionAfterFailure">DELETE</parameter>
<description/>
</proxy>
I can see the string "===555=== true" in the log file, it means that com.fx.FileIsOBI == true. But my file wasn't moved to "/home/user/to/". I think the problem is in
<property name="OUT_ONLY" value="true" scope="default"/>
<property xmlns:ns="http://org.apache.synapse/xsd"
xmlns:ns3="http://org.apache.synapse/xsd"
name="transport.vfs.ReplyFileName"
expression="$trp:FILE_NAME"
scope="transport"/>
<send>
<endpoint>
<address uri="vfs:file:////home/user/to"/>
</endpoint>
</send>
What's wrong?
Please check if VFS transport is enabled,
https://docs.wso2.com/display/ESB490/VFS+Transport
Following works fine for me
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="fromto"
statistics="enable"
trace="enable"
startOnLoad="true">
<target>
<inSequence>
<log level="custom">
<property name="OUTPUT" value="===555==="/>
</log>
<property name="OUT_ONLY" value="true" scope="default"/>
<property xmlns:ns="http://org.apache.synapse/xsd"
xmlns:ns3="http://org.apache.synapse/xsd"
name="transport.vfs.ReplyFileName"
expression="$trp:FILE_NAME"
scope="transport"/>
<send>
<endpoint>
<address uri="vfs:file:///Users/lahiru/work/wso2/esb/9/to"/>
</endpoint>
</send>
</inSequence>
</target>
<parameter name="transport.vfs.ActionAfterProcess">DELETE</parameter>
<parameter name="transport.PollInterval">10</parameter>
<parameter name="transport.vfs.FileURI">vfs:file:///Users/lahiru/work/wso2/esb/9/from</parameter>
<parameter name="transport.vfs.FileNamePattern">.*\.txt</parameter>
<parameter name="transport.vfs.ContentType">application/octet-stream</parameter>
<parameter name="transport.vfs.ActionAfterFailure">DELETE</parameter>
<description/>
</proxy>
I think there is an issue in your file path. Could you change it as below.
<parameter name="transport.vfs.FileURI">vfs:file:///home/user/from/</parameter>
<send>
<endpoint>
<address uri="vfs:file:///home/user/to"/>
</endpoint>
</send>
More over since you are processing an xml file change the content type as follows.
<parameter name="transport.vfs.FileNamePattern">.*\.xml</parameter>
<parameter name="transport.vfs.ContentType">text/xml</parameter>
Do these suggested changes and let us know your feedback.
I am trying to enable Common.Logging.Log4Net to write all types of logs to a log file. The tutorials make it look so simple but I don't know what I am doing wrong. These are the steps I am taking:
Create a new ASP.NET MVC empty project
Install the "Common Logging Log4Net 1211" NuGet package
Add the following lines to the default web.config:
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
</root>
<appender name="FileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="C:\Users\MyName\Downloads\log.txt" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
Make sure the app. pool identity account has RW access to the path where I am trying to save the log file.
Throw a random exception from code for testing purposes.
Am I missing anything? Is there a way I can debug log4net? Please help this poor soul. Thank you.
The issue was related to the NuGet package for "Common.Logging.Log4Net" changing the name of the assembly. This actually fixed it (plesae note the new assembly name, being Common.Logging.Log4Net1211):
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>
https://stackoverflow.com/a/756241/3469201
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
Then, to determine the file in which you want to save the output you can add the following code in the same .config file:
<configuration>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
...
</configuration>
I'm not familiar with Common.Logging.Log4Net. Here is the simple log4net steps that I use -
Install log4net from NuGet.
Insert the following settings in web.config (you can configure the way you want)
Then log.
If you want to use IoC container, you can read my question I asked last month.
web.config
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="FileAppender"/>
</root>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
Testing inside Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
XmlConfigurator.Configure();
// You can inject it to your code using IoC container.
var logger = LogManager.GetLogger(typeof(MvcApplication));
logger.Debug("Test Debug.");
logger.Error("Test Error.");
logger.Fatal("Test Fatal.");
logger.Info("Test Info.");
}
}
Result
2015-06-04 11:48:05,885 [1] DEBUG DemoLog4Net.MvcApplication [(null)] - Test Debug.
2015-06-04 11:48:05,921 [1] ERROR DemoLog4Net.MvcApplication [(null)] - Test Error.
2015-06-04 11:48:05,922 [1] FATAL DemoLog4Net.MvcApplication [(null)] - Test Fatal.
2015-06-04 11:48:05,922 [1] INFO DemoLog4Net.MvcApplication [(null)] - Test Info.
What I'm trying to do is transform one of appSettings which is in external file:
Here is external.config
<?xml version="1.0"?>
<appSettings>
<add key="SomeKey" value="some value" />
</appSettings>
Web.config
<?xml version="1.0"?>
<configuration>
<appSettings file="..\..\external.config">
<add key="SomeKey1" value="some value 1" />
</appSettings>
</configuration>
Web.Debug.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="SomeKey" value="some changed value"xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
After build in proper configuration which in my example is Debug there's only this:
<?xml version="1.0"?>
<configuration>
<appSettings file="..\..\external.config">
<add key="SomeKey1" value="some value 1" />
</appSettings>
</configuration>
but it should be:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="SomeKey1" value="some value 1" />
<add key="SomeKey" value="some changed value"/>
</appSettings>
</configuration>
I tryed to make shared appSettings by 2 or more different project 1-st is WCF Service second ASP.NET MVC 4 Application
Edited:
I've tryed to move this file attribute to Web.Debug.config but it's as well doesn't work.
The question is:
How can i accomplish such thing?Is it even possible?
Interesting. I have the same issue like yours. So now here is a workaround for your reference.
Please open project file - XXX.csproj
for example, ISWB.Test.Unit.csproj
add below section like this
<!-- Rock Add here, 2015.03.19 enable the external config transformation -->
<Target Name="BeforeCompile" Condition="Exists('ISWB.$(Configuration).config')">
<!--Generate transformed app config in the intermediate directory-->
<TransformXml Source="ISWB.config" Destination="$(IntermediateOutputPath)ISWB.config" Transform="ISWB.$(Configuration).config" />
<!--Force build process to use the transformed configuration file from now on.-->
<ItemGroup>
<AppConfigWithTargetPath Remove="ISWB.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)ISWB.config">
<TargetPath>ISWB.config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
<Target Name="AfterCompile" Condition="Exists('app.$(Configuration).config')">
<!--Generate transformed app config in the intermediate directory-->
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
<!--Force build process to use the transformed configuration file from now on.-->
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
Please notice the added section, you have to add it into cs project file in a TEXT editor manually.
Please replace ISWB with yours. And then save it.
it should work well.
Enjoy it!
I need write the response message to a "response.xml" file. And the response should be appended to this file, I set parameter "Append" true, but no use, is completely overwritten every time. Anyone can help me? Thanks and best regards.
<proxy xmlns="http://ws.apache.org/ns/synapse" name="EndpointTest" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<log level="full" />
</inSequence>
<outSequence>
<log level="full" />
<send>
<endpoint>
<address uri="vfs:file:///usr/local/wso2esb-4.0.3/Log" format="pox" />
<property name="OUT_ONLY" value="true" />
<property name="transport.vfs.Append" value="true" />
<property name="transport.vfs.ReplyFileName" value="response.xml" />
</endpoint>
</send>
<send />
</outSequence>
<endpoint>
<address uri="http://172.21.13.151:18989/aaadc/services/receiveMsg" format="pox" />
</endpoint>
</target>
</proxy>
Remove the append= true parameter from service level and use with endpoint url
eg:
vfs:file:///usr/local/wso2esb-4.0.3/Log?transport.vfs.Append=true