console application works only from vs2010 - c#

I wrote a console application project.
When I debug it through visual studio 2010 it runs and performs as needed.
When I run its exe from the cmd,
I don't see the log4net that (are redirected to the console)
No errors are shown on the console
The DB isn't updated.
What can cause this?
32bit proccess vs. 64?
My build is to 32bit.
I run on win7 with VS2010 dotNet 4
update:
My exe works.
but I cannot attach-to-it via visual studio
Log4net doesn't show logs to the console
this is my config file btw
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="DBSubscriptionStorageConfig"
type="NServiceBus.Config.DBSubscriptionStorageConfig, NServiceBus.Core" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<connectionStrings>
<add name="ToolbarsDB" connectionString="server=DEV-DBSRV50;database=TOOLBARSDB;PASSWORD=toolbarsapp;UID=toolbarsapp" providerName="System.Data.SqlClient" />
</connectionStrings>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
<level value="ERROR" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
<level value="FATAL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout" xmlns="">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
<!--Publisher-->
<DBSubscriptionStorageConfig>
<NHibernateProperties>
<add Key="connection.provider"
Value="NHibernate.Connection.DriverConnectionProvider"/>
<add Key="connection.driver_class"
Value="NHibernate.Driver.SqlClientDriver"/>
<add Key="connection.connection_string"
Value="Data Source=DEV-DBSRV80;Initial Catalog=CPServicesDB;Persist Security Info=True;User ID=CPServicesDBUser;Password=oire^3jd!"/>
<add Key="dialect"
Value="NHibernate.Dialect.MsSql2005Dialect"/>
</NHibernateProperties>
</DBSubscriptionStorageConfig>
<!-- End Publisher-->
<appSettings>
<add key="assemblyName" value="Conduit.CPServices.Logic.Bundlator"/>
<add key="typeName" value="Conduit.CPServices.Logic.Bundlator.BundlatorMessageHandlers"/>
</appSettings>
<runtime>
<loadFromRemoteSources enabled="true"/>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NHibernate" publicKeyToken="AA95F207798DFDB4" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.2.0.4000" newVersion="3.2.0.4000"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="CPServicesGeneralServiceBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

If I understood you correctly you do not see the output when you run the application in Command Prompt, but it works fine in Debug Mode.
Are you reading the settings from a app.config by any chance? Maybe you need to copy the Application XML Configuration File as well as the Executable.
The file is normally called MyApplication.exe.config.

Related

log4net not creating output file

I am new to logging and trying out with different appenders. I am trying to output the logs to a log file at a specific location. Though code is compiling and running fine. It is also showing Console logs but it is not adding those logs to output file. Here's my code:
Console file:
namespace LoggerTest
{
class Program
{
private static readonly ILog log = LogManager.GetLogger("General");
static void Main(string[] args)
{
GlobalContext.Properties["logFilePath"] = #"C:\Users\Z004N84C\Desktop\Output.log";
XmlConfigurator.Configure();
log.Info("This is info log");
}
}
}
and here's my app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.ConsoleAppender">
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger %method [%line] - %message%newline" />
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="%property{logFilePath}/ToolLog.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger.%method [%line] - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
In above file I have added log4net configs.
I am not sure what I am missing.
I tried various configs but not able to figure this out. Need to create output file.

No log file when using log4net with XML configuration

I have a problem logging to a file when using log4net and my XML configuration in the app.config below. The logfile doesn't appear on the filesystem, so there is probably something wrong, but I can't figure out what it might be.
The code and configuration is shown below.
App.config
</configSections>
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="example.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<bindings />
<client />
</system.serviceModel>
<applicationSettings>
<NetSuiteClient.Properties.Settings>
<setting name="NetSuiteClient_com_netsuite_webservices_NetSuiteService"
serializeAs="String">
<value>https://webservices.netsuite.com/services/NetSuitePort_2016_1</value>
</setting>
</NetSuiteClient.Properties.Settings>
</applicationSettings>
</configuration>
Declaring:
private static readonly ILog _Log = LogManager.GetLogger(typeof(Form));
Calling:
private void Form1_Load(object sender, EventArgs e)
{
_Log.Info("Info");
_Log.Debug("Debug");
_Log.Error("Error");
}
Are you instructing Log4Net to look for its configuration in the app.config file? You can do that by using the following assembly-level attribute:
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
Example taken from here

log4net log file is not getting created in C#

log file is not getting created,
using below call to get logger and config file.
is it required to give full path for file?
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" requirePermission="false"/>
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="50" />
<maximumFileSize value="50MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Does your code call
log4net.Config.XmlConfigurator.Configure();
on startup?
The <root> section I use has priority, not level:
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
(Apparently, that's not the issue https://stackoverflow.com/a/24188507/21336)
after adding below line in AssemblyInfo.cs, could see log file getting created and logs coming.
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

log4net c#, the installed program cannot write log to file

I can run program in VS2012 (Winform) in both release and debug mode.
It works and writes log to file.
After I compiled to installable program with INNO Compiler. Then install complied program successful. The program works but no log file.
I change permission to full control and ownership. tried run as Administrator.
Still doesn't work.
Thank you.
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\\printer\\log_"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="yyyy-MM-dd.\tx\t" />
<param name="StaticLogFileName" value="false"/>
<lockingmodel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<root>
<priority value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
<category name="DesktopLogger.Form1">
<priority value="ALL" />
</category>
You problem is only guessing without access to your installer/machine. You can enable log4net logging to see what is going on:
<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>
Log4net FAQ

Why my dotnetOpenAuth fails using log4net?

I'm trying to debug why I get this error:
Error occurred while sending a direct message or getting the response.
on this line:
consumer.Channel.Send(consumer.PrepareRequestUserAuthorization(authCallbakUrl, null, null));
So I added log4net, but it doesn't work for me.
My web config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- Others sections -->
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler" requirePermission="false" />
</configSections>
<!-- log4net is a 3rd party (free) logger library that dotnetopenid will use if present but does not require. -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="RelyingParty.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date (GMT%date{%z}) [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="TracePageAppender" type="OpenIdRelyingPartyWebForms.Code.TracePageAppender, OpenIdRelyingPartyWebForms">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date (GMT%date{%z}) [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="TracePageAppender" />
</root>
<!-- Specify the level for some specific categories -->
<logger name="DotNetOpenAuth">
<level value="ALL" />
</logger>
</log4net>
Tried with and without this:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="log4net" />
<bindingRedirect newVersion="4.0.30319" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
I configure the log4net instance
log4net.Config.XmlConfigurator.Configure();
Your binding redirect suggests you're using log4net v4.0.30319, which doesn't exist. The latest version is 1.2.11, and the version DNOA is compiled against is 1.2.10. Once you fix the binding redirect I suspect it will work.
GlobalApplication.cs:
decorate it
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
To get a reference to logger from other places ex.:
ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

Categories